
class Solution {
public int sumOfLeftLeaves1(TreeNode root) {
if(root==null) return 0;
int leftVal = sumOfLeftLeaves(root.left);
int rightVal = sumOfLeftLeaves(root.right);
int medialVal = 0;
if(root.left!=null&&root.left.left==null&&root.left.right==null){
medialVal += root.left.val;
}
return medialVal+leftVal+rightVal;
}
public int sumOfLeftLeaves(TreeNode root) {
if(root==null) return 0;
Stack<TreeNode> stack = new Stack<>();
stack.add(root);
int result = 0;
while(!stack.isEmpty()){
TreeNode node = stack.pop();
if(node.left!=null&&node.left.left==null&&node.left.right==null){
result+=node.left.val;
}
if(node.right!=null) stack.add(node.right);
if(node.left!=null) stack.add(node.left);
}
return result ;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50