当前位置:网站首页>【LeetCode】404. Sum of left leaves (2 brushes of wrong questions)
【LeetCode】404. Sum of left leaves (2 brushes of wrong questions)
2022-07-05 02:20:00 【Kaimar】
- Ideas
Divided into two steps , First, find the left leaf node , Second, sum . Here we are going to use the recursive method of post order traversal . How to judge whether it is left or right , What is needed is the root node to judge !
Recursive parameters and return values : The parameter is the current node , Return value , If the left leaf node returns the current node value , Otherwise return to 0;
The termination condition of recursion : When the node is empty, return ;
Recursive single-layer logic : Use post order traversal , Then the corresponding is left and right middle , The part on the left corresponds to whether it is a leaf node ;
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */
func sumOfLeftLeaves(root *TreeNode) int {
if root == nil {
return 0
}
// Left
leftSum := sumOfLeftLeaves(root.Left)
// Right
rightSum := sumOfLeftLeaves(root.Right)
// in
// 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 .
mid := 0
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
mid = root.Left.Val
}
return leftSum + mid + rightSum
}
边栏推荐
- [download white paper] does your customer relationship management (CRM) really "manage" customers?
- Limited query of common SQL operations
- Practical case of SQL optimization: speed up your database
- Uniapp navigateto jump failure
- 使用druid连接MySQL数据库报类型错误
- Summary and practice of knowledge map construction technology
- Advanced learning of MySQL -- Application -- Introduction
- Win: enable and disable USB drives using group policy
- Numpy library introductory tutorial: basic knowledge summary
- From task Run get return value - getting return value from task Run
猜你喜欢
runc hang 导致 Kubernetes 节点 NotReady
Win: use shadow mode to view the Desktop Session of a remote user
PowerShell: use PowerShell behind the proxy server
Security level
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
Win:使用 PowerShell 检查无线信号的强弱
R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口
Missile interception -- UPC winter vacation training match
[download white paper] does your customer relationship management (CRM) really "manage" customers?
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
随机推荐
openresty ngx_lua变量操作
Win: use shadow mode to view the Desktop Session of a remote user
Interpretation of mask RCNN paper
Educational Codeforces Round 122 (Rated for Div. 2) ABC
Win:使用 PowerShell 检查无线信号的强弱
Prometheus monitors the correct posture of redis cluster
Numpy library introductory tutorial: basic knowledge summary
LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
Win:将一般用户添加到 Local Admins 组中
Limited query of common SQL operations
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
Flutter 2.10 update details
Go RPC call
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
172. Zero after factorial
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Blue bridge - maximum common divisor and minimum common multiple
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
Kotlin - coroutine