当前位置:网站首页>【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
}

边栏推荐
- 官宣!第三届云原生编程挑战赛正式启动!
- Limited query of common SQL operations
- What is the length of SHA512 hash string- What is the length of a hashed string with SHA512?
- Visual explanation of Newton iteration method
- Uniapp navigateto jump failure
- openresty ngx_lua变量操作
- Last week's hot review (2.7-2.13)
- Comment mettre en place une équipe technique pour détruire l'entreprise?
- PowerShell: use PowerShell behind the proxy server
- 220213c language learning diary
猜你喜欢

Security level

Li Kou Jianzhi offer -- binary tree chapter

Mysql database | build master-slave instances of mysql-8.0 or above based on docker

spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time

A label making navigation bar

Bert fine tuning skills experiment

【附源码】基于知识图谱的智能推荐系统-Sylvie小兔

Restful fast request 2022.2.1 release, support curl import

R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口

Runc hang causes the kubernetes node notready
随机推荐
I use these six code comparison tools
Pytorch fine tuning (Fortune): hollowed out design or cheating
Learn game model 3D characters, come out to find a job?
Go RPC call
Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
A tab Sina navigation bar
Abacus mental arithmetic test
187. Repeated DNA sequence - with unordered_ Map basic content
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!
Talk about the things that must be paid attention to when interviewing programmers
Process scheduling and termination
Pgadmin 4 V6.5 release, PostgreSQL open source graphical management tool
Advanced learning of MySQL -- Application -- Introduction
85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
Bert fine tuning skills experiment
Win:使用组策略启用和禁用 USB 驱动器
openresty ngx_lua執行階段
Missile interception -- UPC winter vacation training match
Binary tree traversal - middle order traversal (golang)
官宣!第三届云原生编程挑战赛正式启动!