当前位置:网站首页>Leetcode-404:左叶子之和
Leetcode-404:左叶子之和
2022-07-03 09:22:00 【ITSOK_U】
404. 左叶子之和
题目描述:
给定二叉树的根节点 root ,返回所有左叶子之和。
深度优先(O(N),O(N))
class Solution {
public:
void getLeftNum(TreeNode* root, int &n){
// 根节点为空或者只有一个节点则返回
if(!root->left && !root->right || root == nullptr ) return;
// 该节点有左叶子就加和
else if(root->left != nullptr && root->left->left == nullptr && root->left->right==nullptr) n+=root->left->val;
//否则继续处理左子树
else if(root->left)
getLeftNum(root->left,n);
// 处理右子树
if(root->right)
getLeftNum(root->right,n);
}
int sumOfLeftLeaves(TreeNode* root) {
int n =0 ;
getLeftNum(root,n);
return n;
}
};
广度优先(O(N),O(N))
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(!root->left && !root->right || root==nullptr) return 0;
// 初始化返回值
int res=0;
// 广度优先遍历
queue<TreeNode*> que;
que.push(root);
// 广度优先遍历处理模板
while(!que.empty()){
TreeNode* node = que.front();
que.pop();
// 左孩子入队
if(node->left){
que.push(node->left) ;
// 如果左孩子是叶子节点就加和
if(node->left->left == nullptr && node->left->right == nullptr)
res+=node->left->val;
}
// 右孩子入队
if(node->right) que.push(node->right);
}
return res;
}
};
边栏推荐
- Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
- Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
- Basic use and actual combat sharing of crash tool
- My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
- Yocto Technology Sharing Phase 4: Custom add package support
- Problems encountered when MySQL saves CSV files
- MySQL root user needs sudo login
- Installation and removal of MySQL under Windows
- openCV+dlib实现给蒙娜丽莎换脸
- Adaptiveavgpool1d internal implementation
猜你喜欢
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
Octave instructions
2. Elment UI date selector formatting problem
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
Basic knowledge of communication interface
One click generate traffic password (exaggerated advertisement title)
QT self drawing button with bubbles
Opencv note 21 frequency domain filtering
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
随机推荐
LeetCode - 673. 最长递增子序列的个数
Opencv feature extraction sift
Opencv note 21 frequency domain filtering
1. Finite Markov Decision Process
I think all friends should know that the basic law of learning is: from easy to difficult
Opencv image rotation
Not many people can finally bring their interests to college graduation
Application of 51 single chip microcomputer timer
LeetCode - 705 设计哈希集合(设计)
CV learning notes - image filter
. DLL and Differences between lib files
Do you understand automatic packing and unpacking? What is the principle?
01 business structure of imitation station B project
The data read by pandas is saved to the MySQL database
Sending and interrupt receiving of STM32 serial port
Opencv gray histogram, histogram specification
CV learning notes - camera model (Euclidean transformation and affine transformation)
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
Octave instructions
4G module initialization of charge point design