当前位置:网站首页>[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;
}
}
边栏推荐
- JMeter performance automation test
- The difference between CONDA and pip
- 深入解析kubernetes controller-runtime
- Support vector machine for machine learning
- Use @data in Lombok to simplify entity class code
- Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
- YOLOV3学习笔记
- Project summary --2 (basic use of jsup)
- Various usages of MySQL backup database to create table select and how many days are left
- 2022 cisp-pte (III) command execution
猜你喜欢

Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface

“我为开源打榜狂”第一周榜单公布,160位开发者上榜

.NET程序配置文件操作(ini,cfg,config)

Time format record

Une exploration intéressante de l'interaction souris - pointeur

Redis cluster creation, capacity expansion and capacity reduction

ssh链接远程服务器 及 远程图形化界面的本地显示

SSH link remote server and local display of remote graphical interface

使用conda创建自己的深度学习环境

YOLOV1学习笔记
随机推荐
使用conda创建自己的深度学习环境
【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案
Kubesphere - Multi tenant management
After the Chrome browser is updated, lodop printing cannot be called
Leetcode problem solving summary, constantly updating!
opencv
Various usages of MySQL backup database to create table select and how many days are left
Reinstalling the system displays "setup is applying system settings" stationary
SQL实现将多行记录合并成一行
Migrate data from Mysql to tidb from a small amount of data
YOLOV2学习与总结
【LeetCode】Day93-两个数组的交集 II
Local rviz call and display of remote rostopic
[system design] proximity service
Support vector machine for machine learning
Cesium entity(entities) 实体删除方法
冒泡排序的简单理解
代码管理工具
Get a screenshot of a uiscrollview, including off screen parts
CKA certification notes - CKA certification experience post