当前位置:网站首页>Binary tree - 404. Sum of left leaves
Binary tree - 404. Sum of left leaves
2022-07-26 00:04:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Given the root node of a binary tree root , Returns the sum of all left leaves .
2 Title Example

Example 2:
Input : root = [1]
Output : 0
3 Topic tips
The number of nodes is [1, 1000] Within the scope of
-1000 <= Node.val <= 1000
4 Ideas
A node is 「 Left leaf 」 node , If and only if it is the left child of a node , And it's a leaf node . So we can consider traversing the whole tree , When we traverse to the node node when , If its left child node is a leaf node , Then add up the values of its left child nodes into the answer .
depth :
Complexity analysis
- Time complexity :o(n), among n Is the number of nodes in the tree .
- Spatial complexity :O(n). Spatial complexity is related to the maximum depth of the stack used by depth first search . In the worst case , The tree presents a chain structure , Depth is O(n), The corresponding spatial complexity is also O(n).
Breadth :
Complexity analysis - Time complexity :O(n), among n Is the number of nodes in the tree .
- Spatial complexity :O(n). The spatial complexity is related to the capacity of the queue used by breadth first search , by O(n).
recursive :
The traversal order of recursion is post order traversal ( Right and left ), It is because the sum of the left leaf values is accumulated through the return value of the recursive function ..
Recursive Trilogy :
Determine the parameters and return values of recursive functions
A node and a leaf of the tree , Then you must pass in the root node of the tree , The return value of a recursive function is the sum of numeric values , So for int
Just use the function given in the title .Determine termination conditions
Is stillif (root NLLL) return O ;Determine the logic of monolayer recursion
When you encounter the left leaf node , Record the values , Then the sum of the left leaves of the left subtree is obtained recursively , And the sum of the left leaves of the right subtree , The sum is the sum of the left leaves of the whole tree .
5 My answer
depth :
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
return root != null ? dfs(root) : 0;
}
public int dfs(TreeNode node) {
int ans = 0;
if (node.left != null) {
ans += isLeafNode(node.left) ? node.left.val : dfs(node.left);
}
if (node.right != null && !isLeafNode(node.right)) {
ans += dfs(node.right);
}
return ans;
}
public boolean isLeafNode(TreeNode node) {
return node.left == null && node.right == null;
}
}
Breadth :
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
int ans = 0;
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node.left != null) {
if (isLeafNode(node.left)) {
ans += node.left.val;
} else {
queue.offer(node.left);
}
}
if (node.right != null) {
if (!isLeafNode(node.right)) {
queue.offer(node.right);
}
}
}
return ans;
}
public boolean isLeafNode(TreeNode node) {
return node.left == null && node.right == null;
}
}
Normal recursion :
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
int leftValue = sumOfLeftLeaves(root.left); // Left
int rightValue = sumOfLeftLeaves(root.right); // Right
int midValue = 0;
if (root.left != null && root.left.left == null && root.left.right == null) {
// in
midValue = root.left.val;
}
int sum = midValue + leftValue + rightValue;
return sum;
}
}
边栏推荐
- Song of statistics lyrics
- Stack and queue - 239. Sliding window maximum
- Solve the problem of rapid index bar extrusion
- 栈与队列——150. 逆波兰表达式求值
- 痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
- GUI interface of yolov3 (2) -- beautify the page + output the name and quantity of the identified object
- STM32 lighting procedure
- Iterator pattern of behavioral pattern
- Part 74: overview of machine learning optimization methods and superparameter settings
- A long detailed explanation of C language operators
猜你喜欢

Zhiniu stock -- 09

GUI interface of yolov3 (2) -- beautify the page + output the name and quantity of the identified object

二叉树——404. 左叶子之和

The GUI interface of yolov3 (simple, image detection)

What is inode? It will help you understand inode and what help inode provides when creating and deleting files.

浅识 OWASP

程序环境和预处理

牛市还将继续,拿好手里的币 2021-05-08

二叉树——226. 翻转二叉树

Ten threats to open API ecosystem
随机推荐
SIGIR‘22 推荐系统论文之图网络篇
Are you still using your browser's own bookmarks? This bookmark plugin is awesome
Observer model of behavioral model
Getting started with Servlet
LeetCode 刷题系列 -- 931. 下降路径最小和
STM32 serial port
二叉树——110. 平衡二叉树
牛客/洛谷——[NOIP2003 普及组]栈
ShardingSphere数据分片
Stack and queue - 150. Inverse Polish expression evaluation
Pytoch learning record (I): introduction to pytoch
@Autowired注解的底层原理
二叉树——112. 路径总和
MySQL的DDL、DML和DQL的基本语法
What is inode? It will help you understand inode and what help inode provides when creating and deleting files.
Lua脚本编写Wireshark插件解析第三方私有协议
本轮牛市还能持续多久?|疑问解答 2021-05-11
2022-07-18 study notes of group 5 self-cultivation class (every day)
The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
The items of listview will be displayed completely after expansion