当前位置:网站首页>[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;
}
}
边栏推荐
- Local rviz call and display of remote rostopic
- Mysql database table export and import with binary
- ruoyi接口权限校验
- A letter to graduating college students
- “我为开源打榜狂”第一周榜单公布,160位开发者上榜
- Various usages of MySQL backup database to create table select and how many days are left
- [leetcode] day93 - intersection of two arrays II
- Advanced technology management - do you know the whole picture of growth?
- Jackson: what if there is a lack of property- Jackson: What happens if a property is missing?
- Leetcode problem solving summary, constantly updating!
猜你喜欢

2022 cisp-pte (III) command execution

JMeter performance automation test

Oauth2.0 - using JWT to replace token and JWT content enhancement

Scripy learning

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

Chapter 8. MapReduce production experience

Docker advanced learning (container data volume, MySQL installation, dockerfile)

Kubesphere - set up redis cluster

Phpstudy setting items can be accessed by other computers on the LAN

Method of converting GPS coordinates to Baidu map coordinates
随机推荐
Kubernetes notes (10) kubernetes Monitoring & debugging
Difference between shortest path and minimum spanning tree
方差迭代公式推导
【无标题】8 简易版通讯录
10万奖金被瓜分,快来认识这位上榜者里的“乘风破浪的姐姐”
YOLOV3学习笔记
Migrate data from Amazon aurora to tidb
Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
Phpstudy setting items can be accessed by other computers on the LAN
PMP notes
第8章、MapReduce 生产经验
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle
Zhiniu stock -- 03
Simple understanding of ThreadLocal
After the Chrome browser is updated, lodop printing cannot be called
2022 CISP-PTE(三)命令执行
致即将毕业大学生的一封信
Heap sort and priority queue
Interface test weather API
Redis cluster creation, capacity expansion and capacity reduction