当前位置:网站首页>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;
}
};
边栏推荐
- yocto 技術分享第四期:自定義增加軟件包支持
- 01 business structure of imitation station B project
- 4G module IMEI of charging pile design
- 4G module board level control interface designed by charging pile
- Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
- Google browser plug-in recommendation
- LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
- 4G module at command communication package interface designed by charging pile
- 4.1 Temporal Differential of one step
- Design of charging pile mqtt transplantation based on 4G EC20 module
猜你喜欢

openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹

03 fastjason solves circular references

LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)

1. Finite Markov Decision Process

LeetCode - 715. Range 模块(TreeSet) *****

CV learning notes - deep learning

2021-10-27

Leetcode interview question 17.20 Continuous median (large top pile + small top pile)

Label Semantic Aware Pre-training for Few-shot Text Classification

My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
随机推荐
CV learning notes - reasoning and training
ADS simulation design of class AB RF power amplifier
STM32 running lantern experiment - library function version
2021-11-11 standard thread library
Drive and control program of Dianchuan charging board for charging pile design
QT self drawing button with bubbles
My notes on intelligent charging pile development (II): overview of system hardware circuit design
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Serial port programming
Design of charging pile mqtt transplantation based on 4G EC20 module
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Stm32 NVIC interrupt priority management
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
Opencv gray histogram, histogram specification
Replace the files under the folder with sed
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
Timer and counter of 51 single chip microcomputer
Leetcode-100: same tree
20220610其他:任务调度器
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *