LeetCode-404. Sum of Left Leaves
来源:互联网 发布:java并发最好的书籍 编辑:程序博客网 时间:2024/05/13 03:22
问题:https://leetcode.com/problems/sum-of-left-leaves/?tab=Description
Find the sum of all left leaves in a given binary tree.求左叶子节点的和。
Example:
3
/ \
9 20
/ \
15 7
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
分析:递归,逐个节点查找,如果有左节点就加上它的值。
参考C++代码:
/** * 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: int sumOfLeftLeaves(TreeNode* root) { if(root==NULL) return 0; if(root->left !=NULL && root->left->left==NULL && root->left->right==NULL) return root->left->val+sumOfLeftLeaves(root->right); return sumOfLeftLeaves(root->right)+sumOfLeftLeaves(root->left); }};
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
- 对slideToggle()的进一步了解
- 给定一个字符串,求它的最长回文子串的长度,并打印出最长回文子串
- CentOS7之Jetty安装配置
- 用图片定位html背景
- 设计模式——状态模式(State Pattern)
- LeetCode-404. Sum of Left Leaves
- treeAggregate、treeReduce
- JAVA 入坑教程 | 章节八 什么是Number 类?
- 【译】Nodejs最好的ORM
- WPF下OpenFileDialog实现同一个对话框里打开文件或文件目录
- JSP+Servlet培训班作业管理系统[3]-点击菜单加载功能页面
- Android 平台上的秒级编译方案
- oracle 查询5分钟数据中的整点数据
- 【sqlserver】【问题解决】Cannot execute as the database principal because the principal