Sum of Left Leaves
2016-12-08 21:01
211 查看
Find the sum of all left leaves in a given binary tree.
Example:
求树的所有左叶子的和
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 SumofLeftLeaves { public int sumOfLeftLeaves(TreeNode root) { int sum = 0; if (root == null) return 0; if (isLeave(root.left)) sum += root.left.val; sum += sumOfLeftLeaves(root.left); sum += sumOfLeftLeaves(root.right); return sum; } public boolean isLeave(TreeNode node) { if (node == null) return false; if (node.left == null && node.right == null) return true; return false; } }
相关文章推荐
- LeetCode@HashMap_404_Sum_of_Left_Leaves
- leetcode 404 Sum of Left Leaves
- [LeetCode] Sum of Left Leaves 左子叶之和
- Sum of Left Leaves
- Sum of Left Leaves
- LEETCODE--Sum of Left Leaves
- LeetCode 404 Sum of Left Leaves (DFS)
- Reorder List and Sum of Left Leaves
- Sum of Left Leaves
- 【LeetCode】404 Sum of Left Leaves(java实现)
- LeetCode-404-Sum of Left Leaves-E
- LeetCode 404 Sum of Left Leaves
- Sum of Left Leaves
- LeetCode-Sum of Left Leaves
- Leetcode Sum of Left Leaves
- leetcode 404 Sum of Left Leaves
- Sum of Left Leaves
- LeetCode #404 - Sum of Left Leaves - Easy
- LeetCode 404 Sum of Left Leaves
- Sum of Left Leaves