当前位置:网站首页>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;
}
}
边栏推荐
- C language implementation of three chess
- SIGIR '22 recommendation system paper graph network
- The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
- Getaverse,走向Web3的远方桥梁
- Kubernetes网络插件详解 - Calico篇 - 概述
- 老旧笔记本电脑变服务器(笔记本电脑+内网穿透)
- 抽丝剥茧C语言(高阶)程序环境和预处理
- Key and difficult points of C language pointer
- The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function
- C语言实战之猜拳游戏
猜你喜欢

LeetCode高频题66. 加一,给你一个数组表示数字,则加1返回结果

通货膨胀之下,后市如何操作?2021-05-14

浅识 OWASP

Ten threats to open API ecosystem

How to use yolov5 as an intelligent transportation system for red light running monitoring (1)

Android solves the risk of database injection vulnerability

Dead letter queue and message TTL expiration code

二叉树——111. 二叉树的最小深度

Stm32- analyze latency based on assembly

ShardingSphere数据分片
随机推荐
STM32 timer
STM32 pit encountered when using timer to do delay function
C语言实战之猜拳游戏
STM32 serial port
Key features and application trends of next generation terminal security management
注解@Autowired源码解析
Redis extended data type (jump table /bitmaps/hyperloglog/geospatial)
[learning notes] unreal 4 engine introduction (IV)
Programming password guessing game
What is multithreading
栈与队列——239. 滑动窗口最大值
YoloV4-tiny网络结构
2022-07-18 study notes of group 5 self-cultivation class (every day)
What is inode? It will help you understand inode and what help inode provides when creating and deleting files.
二叉树——404. 左叶子之和
Prometheus operation and maintenance tool promtool (II) query function
C# - readonly 和 const 关键字
Compile live555 with vs2019 in win10
[ManageEngine] servicedesk plus won the 2022 safety model engineering data safety award
Interview focus - TCP protocol of transport layer