当前位置:网站首页>[LeetCode]404. 左叶子之和
[LeetCode]404. 左叶子之和
2022-07-03 06:28:00 【失落的叶】
这是我现在的答案! 2022年6月26日22:33:43
刚刚花了估计不到10min做出来的。
public int sumOfLeftLeaves(TreeNode root) {
//初步依旧是想到用递归中 2022年6月26日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;
}
然后发现我原来代码也太臃肿了吧,根本看不下去!
可读性太差了!
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 0;
}
//叶子节点 是指没有子节点的节点。
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) {
// 左叶子才返回=>被添加到结果中
if (isLeft) {
return node.val;
} else {
return 0;
}
}
int result = 0;
result += sumByRecusiveSelf(node.left, true);
result += sumByRecusiveSelf(node.right, false);
return result;
}
还要判断是否左节点的么,根本不需要的啊,直接null也可以走方法递归的,反正会返回0的啊!
再看下题解!
方法二不放了,用到了栈。
方法一其实和我现在这个写法一样的,好好看下吧。
完毕。
方法一:深度优先搜索
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;
}
}
边栏推荐
- Project summary --2 (basic use of jsup)
- Pdf files can only print out the first page
- [open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle
- Various usages of MySQL backup database to create table select and how many days are left
- Selenium ide installation recording and local project maintenance
- Introduction to software engineering
- Mysql5.7 group by error
- Apifix installation
- Migrate data from Mysql to tidb from a small amount of data
- YOLOV2学习与总结
猜你喜欢
Migrate data from Mysql to tidb from a small amount of data
23 design models
论文笔记 VSALM 文献综述《A Comprehensive Survey of Visual SLAM Algorithms》
Simple understanding of ThreadLocal
Oauth2.0 - Introduction and use and explanation of authorization code mode
Oauth2.0 - using JWT to replace token and JWT content enhancement
剖析虚幻渲染体系(16)- 图形驱动的秘密
Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
远端rostopic的本地rviz调用及显示
After the Chrome browser is updated, lodop printing cannot be called
随机推荐
Kubesphere - build Nacos cluster
[leetcode] day93 - intersection of two arrays II
冒泡排序的简单理解
Pytorch exercise items
Naive Bayes in machine learning
Cesium 点击获三维坐标(经纬度高程)
Use @data in Lombok to simplify entity class code
conda和pip的区别
UNI-APP中条件注释 实现跨段兼容、导航跳转 和 传参、组件创建使用和生命周期函数
简易密码锁
Oracle database synonym creation
phpstudy设置项目可以由局域网的其他电脑可以访问
“我为开源打榜狂”第一周榜单公布,160位开发者上榜
Simple understanding of ThreadLocal
arcgis创建postgre企业级数据库
【系统设计】邻近服务
Mysql database
Support vector machine for machine learning
The difference between CONDA and pip
Cesium entity (entities) entity deletion method