题目和思路
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.
简单的递归
代码
- my
package tree;
/**
* Created by liqiushi on 2018/2/5.
*/
public class SumofLeftLeaves {
public int sumOfLeftLeaves(TreeNode root) {
int sum = 0;
sum = sum(root, sum);
return sum;
}
private int sum(TreeNode root, int sum) {
if (root == null) {
return sum;
}
if(root.left != null ){
if (root.left.left == null&&root.left.right == null) {
sum += root.left.val ;
}else if(root.left.left != null||root.left.right !=null){
sum = sum(root.left, sum);
}
}
if (root.right != null) {
sum = sum(root.right, sum);
}
return sum;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(-9);
TreeNode t1 = new TreeNode(-3);
TreeNode t2 = new TreeNode(2);
TreeNode t3 = new TreeNode(4);
TreeNode t4 = new TreeNode(4);
TreeNode t5 = new TreeNode(0);
TreeNode t6 = new TreeNode(-6);
TreeNode t7 = new TreeNode(-5);
root.left = t1;
root.right = t2;
t1.right = t3;
t2.left = t4;
t2.right = t5;
t3.left = t6;
t4.left = t7;
int result = 0;
result = new SumofLeftLeaves().sumOfLeftLeaves(root);
System.out.println(result);
}
}
- dfs
public class Solution {
public int sumOfLeftLeaves(TreeNode root) {
return dfs(root, false);
}
private int dfs(TreeNode root, boolean isLeft){
if(root == null) return 0;
//是否是叶子节点
if(root.left == null && root.right == null && isLeft) return root.val;
return dfs(root.left, true) + dfs(root.right, false);
}
}
- 非递归
public int sumOfLeftLeaves(TreeNode root) {
if(root == null) return 0;
int ans = 0;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.empty()) {
TreeNode node = stack.pop();
if(node.left != null) {
if (node.left.left == null && node.left.right == null)
ans += node.left.val;
else
stack.push(node.left);
}
if(node.right != null) {
if (node.right.left != null || node.right.right != null)
stack.push(node.right);
}
}
return ans;
}