当前位置:网站首页>【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
}
边栏推荐
- Win:使用组策略启用和禁用 USB 驱动器
- 187. Repeated DNA sequence - with unordered_ Map basic content
- Rabbit MQ message sending of vertx
- Win: enable and disable USB drives using group policy
- Video display and hiding of imitation tudou.com
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
- Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
- 如何搭建一支搞垮公司的技术团队?
- Yolov5 model training and detection
- RichView TRVStyle MainRVStyle
猜你喜欢
R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口
Restful fast request 2022.2.1 release, support curl import
Icu4c 70 source code download and compilation (win10, vs2022)
Li Kou Jianzhi offer -- binary tree chapter
Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!
Can you really learn 3DMAX modeling by self-study?
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Win:使用 PowerShell 检查无线信号的强弱
Application and Optimization Practice of redis in vivo push platform
Summary and practice of knowledge map construction technology
随机推荐
Traditional chips and AI chips
Last week's hot review (2.7-2.13)
Richview trvunits image display units
Yolov5 model training and detection
Leetcode takes out the least number of magic beans
RichView TRVUnits 图像显示单位
Summary and practice of knowledge map construction technology
Blue bridge - maximum common divisor and minimum common multiple
February database ranking: how long can Oracle remain the first?
Unpool(nn.MaxUnpool2d)
From task Run get return value - getting return value from task Run
Huawei machine test question: longest continuous subsequence
Application and development trend of image recognition technology
Advanced conditional statements of common SQL operations
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
Restful fast request 2022.2.1 release, support curl import
STL container
Visual explanation of Newton iteration method
使用druid連接MySQL數據庫報類型錯誤
Go RPC call