1 Star 0 Fork 0

徐长贺/Leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_404.java 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
Fisher Coder 提交于 6年前 . refactor 404
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
/**
* 404. Sum of Left Leaves
*
* Find the sum of all left leaves in a given binary tree.
Example:
3
/ \
9 20
/ \
15 7
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.*/
public class _404 {
public static class Solution1 {
public int sumOfLeftLeaves(TreeNode root) {
int result = 0;
if (root == null) {
return result;
}
return dfs(root, result, false);
}
private int dfs(TreeNode root, int result, boolean left) {
if (root.left == null && root.right == null && left) {
result += root.val;
return result;
}
int leftResult = 0;
if (root.left != null) {
left = true;
leftResult = dfs(root.left, result, left);
}
int rightResult = 0;
if (root.right != null) {
left = false;
rightResult = dfs(root.right, result, left);
}
return leftResult + rightResult;
}
}
public static class Solution2 {
public int sumOfLeftLeaves(TreeNode root) {
int sum = 0;
if (root == null) {
return sum;
}
if (root.left != null) {
if (root.left.left == null && root.left.right == null) {
sum += root.left.val;
} else {
sum += sumOfLeftLeaves(root.left);
}
}
if (root.right != null) {
sum += sumOfLeftLeaves(root.right);
}
return sum;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/isulong/Leetcode.git
git@gitee.com:isulong/Leetcode.git
isulong
Leetcode
Leetcode
master

搜索帮助