404.leetcode Sum of Left Leaves(easy)[二叉树 递归]
来源:互联网 发布:天刀女捏脸数据导入 编辑:程序博客网 时间:2024/04/30 04:32
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.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: void getLeftSum(TreeNode* root,int flag,int &sum) { if(root->left != NULL) { flag = 1; getLeftSum(root->left,flag,sum); } if(root->right != NULL) { flag = 0; getLeftSum(root->right,flag,sum); } if(root->left == NULL && root->right == NULL) { if(flag == 1) sum += root->val; } return; } int sumOfLeftLeaves(TreeNode* root) { if(root == NULL) return 0; int flag = 0; int sum = 0; getLeftSum(root,flag,sum); return sum; }};
0 0
- 404.leetcode Sum of Left Leaves(easy)[二叉树 递归]
- LeetCode 404. Sum of Left Leaves (二叉树,递归)
- LeetCode 404. Sum of Left Leaves(二叉树)
- LeetCode #404 - Sum of Left Leaves - Easy
- Leetcode 404. Sum of Left Leaves (Easy) (cpp)
- 404. Sum of Left Leaves 难度:easy
- LeetCode 404. Sum of Left Leaves 递归终止条件
- 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]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
- 使用Java创建RESTful Web Service
- sqoop exort data from DB2 or Oracle to Hadoop
- Clojure 学习入门(11)- 宏 macro
- 文章标题
- Codeforces Round #358 (Div. 2) E. Alyona and Triangles (找面积最大三角形)
- 404.leetcode Sum of Left Leaves(easy)[二叉树 递归]
- 用树莓派学编程系列3——环境温湿度测量2
- 希尔排序算法
- java常用函数
- 使用jQuery更新和交互
- Android学习计划
- Clojure 学习入门(12)- map/pmap
- Python:继承
- 神经网络NeualNet代码