当前位置:网站首页>[LeetCode]404. Sum of left leaves
[LeetCode]404. Sum of left leaves
2022-07-03 06:42:00 【Lost leaf】
This is my answer now ! 2022 year 6 month 26 Japan 22:33:43
It just took me less than estimated 10min make .
public int sumOfLeftLeaves(TreeNode root) {
// At first, I still think of using recursion 2022 year 6 month 26 Japan 22:23:46
if(root == null){
return 0;
}
int result = 0;
TreeNode left = root.left;
TreeNode right = root.right;
if(left != null){
if(left.left == null && left.right == null){
result += left.val;
}else{
result += sumOfLeftLeaves(left);
}
if(left.left != null){
}
}
result += sumOfLeftLeaves(right);
return result;
}
Then I found that my original code was too bloated , I can't see it at all !
Poor readability !
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 0;
}
// Leaf node A node without children .
int sum = 0;
sum += sumByRecusiveSelf( root.left, true);
sum += sumByRecusiveSelf( root.right, false);
return sum;
}
private int sumByRecusiveSelf(TreeNode node, boolean isLeft) {
if (node == null) {
return 0;
}
if (node.left == null && node.right == null) {
// The left leaf just returns => Added to the result
if (isLeft) {
return node.val;
} else {
return 0;
}
}
int result = 0;
result += sumByRecusiveSelf(node.left, true);
result += sumByRecusiveSelf(node.right, false);
return result;
}
Do you want to judge whether the left node , You don't need it at all , direct null You can also use recursive methods , It will return anyway 0 ah !
Look at the solution !
Method two doesn't go away , The stack is used .
Method 1 is actually the same as my current writing , Take a good look .
complete .
Method 1 : Depth-first search
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;
}
}
边栏推荐
- Pdf files can only print out the first page
- 【无标题】
- 2022 cisp-pte (III) command execution
- How to scan when Canon c3120l is a network shared printer
- Personally design a highly concurrent seckill system
- (翻译)异步编程:Async/Await在ASP.NET中的介绍
- [LeetCode]404. 左叶子之和
- Derivation of variance iteration formula
- Selenium - 改变窗口大小,不同机型呈现的宽高长度会不一样
- 论文笔记 VSALM 文献综述《A Comprehensive Survey of Visual SLAM Algorithms》
猜你喜欢

IE browser flash back, automatically open edge browser

Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)

2022 CISP-PTE(三)命令执行

IC_EDA_ALL虚拟机(丰富版):questasim、vivado、vcs、verdi、dc、pt、spyglass、icc2、synplify、INCISIVE、IC617、MMSIM、工艺库

Summary of remote connection of MySQL

Mysql

23 design models

论文笔记 VSALM 文献综述《A Comprehensive Survey of Visual SLAM Algorithms》

New knowledge! The virtual machine network card causes your DNS resolution to slow down

Numerical method for solving optimal control problem (I) -- gradient method
随机推荐
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle
方差迭代公式推导
Mysql
如何迁移或复制VMware虚拟机系统
“我为开源打榜狂”第一周榜单公布,160位开发者上榜
机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
ssh链接远程服务器 及 远程图形化界面的本地显示
POI dealing with Excel learning
JMeter performance automation test
【LeetCode】Day93-两个数组的交集 II
[Code] if (list! = null & list. Size() > 0) optimization, set empty judgment implementation method
Interface test weather API
代码管理工具
After the Chrome browser is updated, lodop printing cannot be called
Unit test framework + Test Suite
JMeter linked database
Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案
(翻译)异步编程:Async/Await在ASP.NET中的介绍
Simple understanding of bubble sorting