Skip to content

Commit 4448ffc

Browse files
committed
tree
1 parent 63c38f5 commit 4448ffc

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

src/PathSum/Solution.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package PathSum;
2+
3+
import commons.datastructures.TreeNode;
4+
5+
/**
6+
* User: Danyang
7+
* Date: 1/19/2015
8+
* Time: 19:33
9+
* Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
10+
*/
11+
public class Solution {
12+
/**
13+
* dfs
14+
* @param root
15+
* @param sum
16+
* @return
17+
*/
18+
public boolean hasPathSum_error(TreeNode root, int sum) {
19+
if(root==null)
20+
return false;
21+
return dfs(root, 0, sum, 0);
22+
}
23+
24+
boolean dfs(TreeNode cur, int sum_cur, int sum, int depth) {
25+
if(cur==null) {
26+
if(sum_cur==sum&&depth>1)
27+
return true;
28+
else
29+
return false;
30+
}
31+
return dfs(cur.left, sum_cur+cur.val, sum, depth+1) || dfs(cur.right, sum_cur+cur.val, sum, depth+1);
32+
}
33+
34+
35+
public boolean hasPathSum(TreeNode root, int sum) {
36+
return dfs(root, sum);
37+
}
38+
39+
/**
40+
* TreeNode val can be negative
41+
* @param c
42+
* @param remain
43+
* @return
44+
*/
45+
boolean dfs(TreeNode c, int remain) {
46+
if(c==null)
47+
return false;
48+
49+
remain -= c.val;
50+
51+
if(remain==0&&c.left==null&&c.right==null)
52+
return true;
53+
54+
return dfs(c.left, remain) || dfs(c.right, remain);
55+
}
56+
}

src/PathSumII/Solution.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package PathSumII;
2+
3+
import commons.datastructures.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* User: Danyang
10+
* Date: 1/19/2015
11+
* Time: 19:51
12+
*/
13+
public class Solution {
14+
public List<List<Integer>> pathSum(TreeNode root, int sum) {
15+
List<List<Integer>> ret = new ArrayList<>();
16+
dfs(root, new ArrayList<>(), sum, ret);
17+
return ret;
18+
}
19+
20+
void dfs(TreeNode c, List<Integer> cur, int remain, List<List<Integer>> ret) {
21+
if(c==null)
22+
return;
23+
24+
remain -= c.val;
25+
cur.add(c.val);
26+
if(remain==0 && c.left==null && c.right==null) {
27+
List<Integer> t = new ArrayList<>();
28+
t.addAll(cur);
29+
ret.add(t);
30+
}
31+
dfs(c.left, cur, remain, ret);
32+
dfs(c.right, cur, remain, ret);
33+
cur.remove(cur.size()-1);
34+
}
35+
}

src/SymmetricTree/Solution.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package SymmetricTree;
2+
3+
import commons.datastructures.TreeNode;
4+
5+
/**
6+
* User: Danyang
7+
* Date: 1/19/2015
8+
* Time: 19:21
9+
*
10+
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
11+
*/
12+
public class Solution {
13+
/**
14+
* iterative: bfs, array sysmetric
15+
* recursive: drawing the tree
16+
* 1
17+
* 2 2
18+
* 3 4 4 3
19+
* 5 6 7 88 7 6 5
20+
* @param root
21+
* @return
22+
*/
23+
public boolean isSymmetric(TreeNode root) {
24+
if(root==null)
25+
return true;
26+
return isSymmetric(root.left, root.right);
27+
}
28+
29+
boolean isSymmetric(TreeNode l, TreeNode r) {
30+
if(l==null&&r==null)
31+
return true;
32+
try {
33+
return l.val==r.val && isSymmetric(l.left, r.right) && isSymmetric(l.right, r.left);
34+
}
35+
catch(Exception e) {
36+
return false;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)