Leetcode 404(Java)
来源:互联网 发布:网络爬虫 编辑:程序博客网 时间:2024/05/21 19:26
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.
sum为所有左枝叶子结点的和,采用DFS递归调用SumOfLeftLeaves(),判断当前结点左子结点非空后,在判断其是否叶子结点,若是则将其值加入sum中,AC码如下:
public class Solution { int sum=0; public int sumOfLeftLeaves(TreeNode root) { if(root==null)return 0; if(root.left!=null){ if(root.left.left==null && root.left.right==null){ sum+=root.left.val; }else{ sumOfLeftLeaves(root.left); } } sumOfLeftLeaves(root.right); return sum; }}
0 0
- Leetcode 404(Java)
- Leetcode(java)
- leetcode java
- [LeetCode-404]Sum of Left Leaves(java)
- [LeetCode]Next Permutation java leetcode
- LeetCode: Two Sum (Java)
- [Leetcode] Two Sum (Java)
- [Leetcode] ZigZag Conversion (Java)
- [Leetcode] Reverse Integer (Java)
- [Leetcode] Palindrome Number (Java)
- [Leetcode] Valid Parentheses (Java)
- [Leetcode] Generate Parentheses (Java)
- [Leetcode] Remove Element (Java)
- [Leetcode] Implement strStr() (Java)
- [Leetcode] Next Permutation (Java)
- [Leetcode] Valid Sudoku (Java)
- [Leetcode] Sudoku Solver (Java)
- [Leetcode] Combination Sum (Java)
- Java-多线程-并发编程
- 《Data Mining:Concepts and Techniques》翻译与笔记
- Linux编程基础之进程间通信之四:共享内存
- java 远程方法调用示例
- 实体类含有自身或者另一个实体时转json
- Leetcode 404(Java)
- 性能分析之IO分析-jbd2引起的IO高
- Python 小案例 打字练习
- [Leetcode] 147. Insertion Sort List 解题报告
- IP地址最后一位斜杠是什么意思?比如192.168.1.10/27?还有IP地址和子网掩码相加得到的网络地址是什么意思
- Hibernate多对多
- 需求变更,产品经理的良心也会痛!
- BZOJ 3831: [Poi2014]Little Bird
- 十分钟入门Matplotlib