File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Contest/src/_20160924_6th_contest Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ package _20160924_6th_contest ;
2
+
3
+ import classes .TreeNode ;
4
+
5
+ public class SumofLeftLeaves {
6
+ public int sumOfLeftLeaves (TreeNode root ) {
7
+ int result = 0 ;
8
+ if (root == null ) return result ;
9
+ return dfs (root , result , false );
10
+ }
11
+
12
+ private int dfs (TreeNode root , int result , boolean left ) {
13
+ if (root .left == null && root .right == null && left ) {
14
+ result += root .val ;
15
+ return result ;
16
+ }
17
+ int leftResult = 0 ;
18
+ if (root .left != null ){
19
+ left = true ;
20
+ leftResult = dfs (root .left , result , left );
21
+ }
22
+ int rightResult = 0 ;
23
+ if (root .right != null ){
24
+ left = false ;
25
+ rightResult = dfs (root .right , result , left );
26
+ }
27
+ return leftResult + rightResult ;
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments