404:左叶子之和

This commit is contained in:
huangge1199@hotmail.com 2021-07-24 19:05:30 +08:00
parent 7271b462c7
commit ea4cf9f54d
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,67 @@
//计算给定二叉树的所有左叶子之和
//
// 示例
//
//
// 3
// / \
// 9 20
// / \
// 15 7
//
//在这个二叉树中有两个左叶子分别是 9 15所以返回 24
//
//
// Related Topics 深度优先搜索 广度优先搜索 二叉树
// 👍 328 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TreeNode;
//404:左叶子之和
class SumOfLeftLeaves {
public static void main(String[] args) {
//测试代码
Solution solution = new SumOfLeftLeaves().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
int sum = 0;
if (root.left != null) {
if (root.left.left == null && root.left.right == null) {
sum += root.left.val;
}
sum += sumOfLeftLeaves(root.left);
}
if (root.right != null) {
sum += sumOfLeftLeaves(root.right);
}
return sum;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,15 @@
<p>计算给定二叉树的所有左叶子之和。</p>
<p><strong>示例:</strong></p>
<pre>
3
/ \
9 20
/ \
15 7
在这个二叉树中,有两个左叶子,分别是 9 和 15所以返回 24</pre>
<p>&nbsp;</p>
<div><div>Related Topics</div><div><li></li><li>深度优先搜索</li><li>广度优先搜索</li><li>二叉树</li></div></div>\n<div><li>👍 328</li><li>👎 0</li></div>