当前位置:网站首页>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;
}
};
边栏推荐
- My notes on intelligent charging pile development (II): overview of system hardware circuit design
- (2) New methods in the interface
- 4G module IMEI of charging pile design
- Deep Reinforcement learning with PyTorch
- Timer and counter of 51 single chip microcomputer
- Tensorflow built-in evaluation
- Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:
- LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
- Leetcode - 933 number of recent requests
- LeetCode - 919. 完全二叉树插入器 (数组)
猜你喜欢
LeetCode - 900. RLE 迭代器
Deep Reinforcement learning with PyTorch
LeetCode - 673. 最长递增子序列的个数
Yocto Technology Sharing Phase 4: Custom add package support
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
Vgg16 migration learning source code
MySQL root user needs sudo login
CV learning notes - reasoning and training
Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
随机推荐
LeetCode - 933 最近的请求次数
The data read by pandas is saved to the MySQL database
20220610其他:任务调度器
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
YOLO_ V1 summary
Basic use and actual combat sharing of crash tool
Installation and removal of MySQL under Windows
Opencv interview guide
Vgg16 migration learning source code
Vscode markdown export PDF error
Opencv+dlib to change the face of Mona Lisa
Modelcheckpoint auto save model
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
Drive and control program of Dianchuan charging board for charging pile design
Opencv Harris corner detection
Markdown latex full quantifier and existential quantifier (for all, existential)
LeetCode - 919. Full binary tree inserter (array)
getopt_ Typical use of long function
Application of 51 single chip microcomputer timer