【LeetCode】404. Sum of Left Leaves
来源:互联网 发布:网贷安卓源码 编辑:程序博客网 时间:2024/06/08 03:55
Find the sum of all left leaves in a given binary tree.
Example:
3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
方法1:
class Solution {public: int sum=0; void DFS(TreeNode* root,int dir){ if(!root)return; if(dir==1&&!root->left&&!root->right){ sum+=root->val; } DFS(root->left,1); DFS(root->right,2); } int sumOfLeftLeaves(TreeNode* root) { DFS(root,0); return sum; }};
方法2:
class Solution {public: int sumOfLeftLeaves(TreeNode* root) { if(!root)return 0; else if(root->left&&!root->left->right&&!root->left->left) return root->left->val+sumOfLeftLeaves(root->right); else return sumOfLeftLeaves(root->left)+sumOfLeftLeaves(root->right); }};
阅读全文
0 0
- LeetCode笔记:404. Sum of Left Leaves
- 【leetcode】404. Sum of Left Leaves【E】
- LeetCode—404. Sum of Left Leaves
- [LeetCode]404. Sum of Left Leaves
- Leetcode 404. Sum of Left Leaves
- LeetCode 404. Sum of Left Leaves
- 404. Sum of Left Leaves - leetcode
- [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
- LeetCode 404. Sum of Left Leaves
- leetcode 404. Sum of Left Leaves
- leetcode(88).404. Sum of Left Leaves
- 【LeetCode】 404. Sum of Left Leaves
- leetcode 404. Sum of Left Leaves
- [Leetcode]404. Sum of Left Leaves
- 彩色宝石项链C/C++解决
- 1. Two Sum
- KMP算法的一些细节
- JAVA序列化和反序列化的常见格式
- 《.NET 设计规范》第 9 章:常用的设计模式
- 【LeetCode】404. Sum of Left Leaves
- 集合类源码简单阅读(一)(ArrayList)
- MYSQL数据库间同步数据
- 坐标系变换数学基础
- 服务器客户端
- web访问的原理
- centos7下redis莫名的链接不上
- 工厂模式
- GitHub管理代码操作方式