当前位置:网站首页>Leetcode-404: sum of left leaves
Leetcode-404: sum of left leaves
2022-07-03 10:10:00 【ITSOK_ U】
404. Sum of left leaves
Title Description :
Given the root node of a binary tree root , Returns the sum of all left leaves .
Depth first (O(N),O(N))
class Solution {
public:
void getLeftNum(TreeNode* root, int &n){
// If the root node is empty or there is only one node, it returns
if(!root->left && !root->right || root == nullptr ) return;
// Add the left leaf of this node
else if(root->left != nullptr && root->left->left == nullptr && root->left->right==nullptr) n+=root->left->val;
// Otherwise, continue to process the left subtree
else if(root->left)
getLeftNum(root->left,n);
// Deal with the right subtree
if(root->right)
getLeftNum(root->right,n);
}
int sumOfLeftLeaves(TreeNode* root) {
int n =0 ;
getLeftNum(root,n);
return n;
}
};
breadth-first (O(N),O(N))
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(!root->left && !root->right || root==nullptr) return 0;
// Initialization return value
int res=0;
// Breadth first traversal
queue<TreeNode*> que;
que.push(root);
// Breadth first traversal processing template
while(!que.empty()){
TreeNode* node = que.front();
que.pop();
// The left child joined the team
if(node->left){
que.push(node->left) ;
// If the left child is a leaf node, add it
if(node->left->left == nullptr && node->left->right == nullptr)
res+=node->left->val;
}
// The right kid joins the team
if(node->right) que.push(node->right);
}
return res;
}
};
边栏推荐
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- Interruption system of 51 single chip microcomputer
- Retinaface: single stage dense face localization in the wild
- pycharm 无法引入自定义包
- Leetcode-100:相同的树
- CV learning notes - edge extraction
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
- getopt_ Typical use of long function
- When the reference is assigned to auto
猜你喜欢

LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)

pycharm 无法引入自定义包

Opencv image rotation

2021-10-28

Development of intelligent charging pile (I): overview of the overall design of the system

Opencv feature extraction - hog

3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation

CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)

51 MCU tmod and timer configuration

LeetCode - 933 最近的请求次数
随机推荐
Tensorflow2.0 save model
Opencv+dlib to change the face of Mona Lisa
On the problem of reference assignment to reference
yocto 技術分享第四期:自定義增加軟件包支持
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
LeetCode - 715. Range 模块(TreeSet) *****
Window maximum and minimum settings
20220606数学:分数到小数
Swing transformer details-2
CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
【C 题集】of Ⅵ
Serial port programming
Leetcode-513:找树的左下角值
Simulate mouse click
4.1 Temporal Differential of one step
LeetCode - 673. 最长递增子序列的个数
Swing transformer details-1
Opencv interview guide
Label Semantic Aware Pre-training for Few-shot Text Classification
Tensorflow built-in evaluation