当前位置:网站首页>[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;
}
}
边栏推荐
- Zhiniu stock -- 03
- Jackson: what if there is a lack of property- Jackson: What happens if a property is missing?
- Docker advanced learning (container data volume, MySQL installation, dockerfile)
- Read blog type data from mysql, Chinese garbled code - solved
- Oauth2.0 - use database to store client information and authorization code
- Oauth2.0 - Introduction and use and explanation of authorization code mode
- Project summary --2 (basic use of jsup)
- Local rviz call and display of remote rostopic
- Common interview questions
- When PHP uses env to obtain file parameters, it gets strings
猜你喜欢
随机推荐
Reinstalling the system displays "setup is applying system settings" stationary
认识弹性盒子flex
In depth analysis of kubernetes controller runtime
Fluentd facile à utiliser avec le marché des plug - ins rainbond pour une collecte de journaux plus rapide
Various usages of MySQL backup database to create table select and how many days are left
【5G NR】UE注册流程
JMeter linked database
Migrate data from Amazon aurora to tidb
Characteristics and isolation level of database
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle
剖析虚幻渲染体系(16)- 图形驱动的秘密
Advanced technology management - do you know the whole picture of growth?
Use @data in Lombok to simplify entity class code
“我为开源打榜狂”第一周榜单公布,160位开发者上榜
有意思的鼠标指针交互探究
Jackson: what if there is a lack of property- Jackson: What happens if a property is missing?
Zhiniu stock project -- 04
Apifix installation
远端rostopic的本地rviz调用及显示
使用 Abp.Zero 搭建第三方登录模块(一):原理篇









