当前位置:网站首页>【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
}
边栏推荐
- Outlook: always prompt for user password
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
- Official announcement! The third cloud native programming challenge is officially launched!
- Visual explanation of Newton iteration method
- openresty ngx_ Lua variable operation
- 85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
- Li Kou Jianzhi offer -- binary tree chapter
- Word processing software
- Tla+ through examples (XI) -- propositional logic and examples
猜你喜欢
Application and Optimization Practice of redis in vivo push platform
Learn game model 3D characters, come out to find a job?
Yolov5 model training and detection
Restful fast request 2022.2.1 release, support curl import
He was laid off.. 39 year old Ali P9, saved 150million
如何做一个炫酷的墨水屏电子钟?
openresty ngx_ Lua execution phase
Three properties that a good homomorphic encryption should satisfy
如何搭建一支搞垮公司的技术团队?
[uc/os-iii] chapter 1.2.3.4 understanding RTOS
随机推荐
Yolov5 model training and detection
February database ranking: how long can Oracle remain the first?
[機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
R language uses logistic regression and afrima, ARIMA time series models to predict world population
力扣剑指offer——二叉树篇
Runc hang causes the kubernetes node notready
Three properties that a good homomorphic encryption should satisfy
Open source SPL optimized report application coping endlessly
Traditional chips and AI chips
A label making navigation bar
Exploration of short text analysis in the field of medical and health (II)
Win: enable and disable USB drives using group policy
Learn tla+ (XII) -- functions through examples
Huawei machine test question: longest continuous subsequence
85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
Android advanced interview question record in 2022
Bert fine tuning skills experiment
Learn game model 3D characters, come out to find a job?
CAM Pytorch
Asynchronous and promise