leetcode 404 Sum of Left Leaves
2017-02-06 00:19
288 查看
Problem:
给一颗二叉树,求这棵树的左叶子节点之和。
Solution:
dfs搜一下即可。
notes:
1. 默认参数的应用。
2. 要注意root可能为空,如果为空则不能方位它的左值,所以要处理这个异常。
给一颗二叉树,求这棵树的左叶子节点之和。
Solution:
dfs搜一下即可。
notes:
1. 默认参数的应用。
2. 要注意root可能为空,如果为空则不能方位它的左值,所以要处理这个异常。
class Solution { public: int sumOfLeftLeaves(TreeNode* root, bool isLeft = false) { int ans = 0; if(!root) return 0; if(isLeft && !root->left && !root->right) ans += root->val; else { ans += sumOfLeftLeaves(root->left, true); ans += sumOfLeftLeaves(root->right); } return ans; } };
相关文章推荐
- Leetcode #404 Sum of Left Leaves
- [LeetCode-404]Sum of Left Leaves(java)
- leetcode 404 Sum of Left Leaves C++
- LeetCode@Tree_404_Sum_of_Left_Leaves
- LeetCode 404 Sum of Left Leaves
- LeetCode-404-Sum of Left Leaves-E
- LeetCode[404] Sum of Left Leaves
- LeetCode@HashMap_404_Sum_of_Left_Leaves
- LeetCode 404: Sum of Left Leaves
- leetcode404: Sum of Left Leaves
- LeetCode 404:Sum of Left Leaves
- leetcode 404 Sum of Left Leaves
- LeetCode 404 Sum of Left Leaves
- leetcode 404 Sum of Left Leaves
- 【LeetCode】404 Sum of Left Leaves(java实现)
- leetcode-404-Sum of Left Leaves
- [Leetcode] #404 Sum of Left Leaves
- leetcode(404):Sum of Left Leaves
- Leetcode 404 Sum of Left Leaves
- 【LeetCode】404 Sum of Left Leaves(java实现)