Skip to content

Commit b328a96

Browse files
author
rchen102
committed
Tree and backtrack
1 parent fb97f41 commit b328a96

18 files changed

+343
-258
lines changed

Java/leetcode 1022.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int sumRootToLeaf(TreeNode root) {
3+
return dfs(root, 0);
4+
}
5+
6+
private int dfs(TreeNode cur, int pathSum) {
7+
if (cur == null) return pathSum;
8+
pathSum = (pathSum << 1) + cur.val;
9+
if (cur.left == null && cur.right == null) return pathSum;
10+
int res = 0;
11+
if (cur.left != null) res += dfs(cur.left, pathSum);
12+
if (cur.right != null) res += dfs(cur.right, pathSum);
13+
return res;
14+
}
15+
}

Java/leetcode 112.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,46 @@
77
* TreeNode(int x) { val = x; }
88
* }
99
*/
10-
//Solution1: recursive dfs T: O(n) S: O(logn)
10+
//Solution1: recursive post-order T: O(n) S: O(logn)
1111
class Solution {
1212
public boolean hasPathSum(TreeNode root, int sum) {
13-
if(root == null) return false;
14-
if(root.left == null && root.right == null) return root.val == sum;
15-
else return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
13+
if (root == null) return false;
14+
return dfs(root, 0, sum);
15+
}
16+
17+
// pathSum: current path sum, not include cur
18+
private boolean dfs(TreeNode cur, int pathSum, int sum) {
19+
if (cur == null) return false;
20+
pathSum += cur.val;
21+
if (cur.left == null && cur.right == null) {
22+
return pathSum == sum;
23+
}
24+
return dfs(cur.left, pathSum, sum) || dfs(cur.right, pathSum, sum);
1625
}
1726
}
1827

1928
//Solution2: iterative dfs T: O(n) S: O(n/2)
2029
class Solution {
2130
public boolean hasPathSum(TreeNode root, int sum) {
2231
if (root == null) return false;
23-
Stack<TreeNode> stackNode = new Stack<>();
24-
Stack<Integer> stackSum = new Stack<>();
25-
stackNode.push(root);
26-
stackSum.push(root.val);
27-
while (!stackNode.isEmpty()) {
28-
TreeNode cur = stackNode.pop();
29-
int tmp = stackSum.pop();
30-
// Check whether it is a leaf node
32+
Stack<TreeNode> stack = new Stack<>();
33+
Stack<Integer> sumStack = new Stack<>();
34+
35+
stack.push(root);
36+
sumStack.push(root.val);
37+
while (!stack.isEmpty()) {
38+
TreeNode cur = stack.pop();
39+
int curSum = sumStack.pop();
3140
if (cur.left == null && cur.right == null) {
32-
if (tmp == sum) return true;
33-
} else {
34-
if (cur.right != null) {
35-
stackNode.push(cur.right);
36-
stackSum.push(tmp + cur.right.val);
37-
}
38-
if (cur.left != null) {
39-
stackNode.push(cur.left);
40-
stackSum.push(tmp + cur.left.val);
41-
}
41+
if (curSum == sum) return true;
42+
}
43+
if (cur.right != null) {
44+
stack.push(cur.right);
45+
sumStack.push(curSum + cur.right.val);
46+
}
47+
if (cur.left != null) {
48+
stack.push(cur.left);
49+
sumStack.push(curSum + cur.left.val);
4250
}
4351
}
4452
return false;

Java/leetcode 113.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1-
//Solution1: recursive backtracking dfs T: O(n) S: O(n) worst
1+
// Solution1: recursive backtrace dfs T: O(n) S: O(n) worst
22
class Solution {
33
public List<List<Integer>> pathSum(TreeNode root, int sum) {
44
List<List<Integer>> res = new ArrayList<>();
5-
helper(root, sum, res, new ArrayList<>());
5+
dfs(root, sum, new ArrayList<>(), res);
66
return res;
77
}
88

9-
public void helper(TreeNode root, int sum, List<List<Integer>> res, List<Integer> tmp) {
10-
if (root == null) return;
11-
tmp.add(root.val);
12-
if (root.left == null && root.right == null) { // This is a leaf
13-
if (root.val == sum) res.add(new ArrayList<>(tmp));
14-
} else { // This is not a leaf
15-
helper(root.left, sum - root.val, res, tmp);
16-
helper(root.right, sum - root.val, res, tmp);
9+
private void dfs(TreeNode cur, int remain, List<Integer> curPath, List<List<Integer>> res) {
10+
if (cur == null) return;
11+
// update vars with the new step
12+
curPath.add(cur.val);
13+
remain -= cur.val;
14+
15+
// do check for current step
16+
if (cur.left == null && cur.right == null) {
17+
if (remain == 0) {
18+
res.add(new ArrayList<>(curPath));
19+
}
1720
}
18-
tmp.remove(tmp.size() - 1);
21+
22+
dfs(cur.left, remain, curPath, res);
23+
dfs(cur.right, remain, curPath, res);
24+
25+
// back one step
26+
curPath.remove(curPath.size() - 1);
1927
}
2028
}

Java/leetcode 114.java

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,54 @@
77
* TreeNode(int x) { val = x; }
88
* }
99
*/
10-
//Solution1: recursive T: O(n) S: O(h)
10+
// Solution1: post-order dfs T: O(n) S: O(h)
1111
class Solution {
1212
public void flatten(TreeNode root) {
13-
helper(root, null);
13+
dfs(root, null);
1414
}
1515

16-
private TreeNode helper(TreeNode cur, TreeNode prev) {
17-
if(cur == null) return prev;
18-
prev = helper(cur.right, prev);
19-
prev = helper(cur.left, prev);
20-
cur.right = prev;
21-
cur.left = null;
22-
prev = cur;
23-
return prev;
16+
/**
17+
* 1. flatten root
18+
* 2. link the list after root
19+
* 3. return the newHead
20+
*/
21+
public TreeNode dfs(TreeNode root, TreeNode linkedList) {
22+
if (root == null) return linkedList;
23+
linkedList = dfs(root.right, linkedList);
24+
linkedList = dfs(root.left, linkedList);
25+
root.right = linkedList;
26+
root.left = null;
27+
linkedList = root;
28+
return linkedList;
2429
}
2530
}
2631

27-
//Solution2: iterative pre-order search T: O(n) S: O(h)
32+
// Solution2: iterative pre-order T: O(n) S: O(h)
2833
class Solution {
2934
public void flatten(TreeNode root) {
30-
if(root == null) return;
31-
Stack<TreeNode> stack = new Stack<TreeNode>();
32-
TreeNode start = new TreeNode(0);
33-
34-
TreeNode cur;
35+
if (root == null) return;
36+
Stack<TreeNode> stack = new Stack<>();
3537
stack.push(root);
36-
while(!stack.empty()) {
37-
cur = stack.pop();
38-
start.right = cur;
39-
start = cur;
40-
if(cur.right != null)
41-
stack.push(cur.right);
42-
if(cur.left != null) {
43-
stack.push(cur.left);
44-
cur.left = null;
38+
TreeNode dummy = new TreeNode();
39+
TreeNode prev = dummy;
40+
while (!stack.isEmpty()) {
41+
TreeNode cur = stack.pop();
42+
TreeNode left = cur.left;
43+
TreeNode right = cur.right;
44+
// do some with cur
45+
prev.right = cur;
46+
cur.left = null;
47+
cur.right = null;
48+
prev = cur;
49+
// push left and child
50+
if (right != null) {
51+
stack.push(right);
52+
}
53+
if (left != null) {
54+
stack.push(left);
4555
}
46-
}
56+
}
57+
prev.right = null;
4758
}
4859
}
4960

Java/leetcode 129.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@
77
* TreeNode(int x) { val = x; }
88
* }
99
*/
10-
//Solution1: recursive dfs T: O(n) S: O(h) h = logn
10+
// Solution1: pre-order recursive T: O(n) S: O(h) h = logn
1111
class Solution {
1212
public int sumNumbers(TreeNode root) {
13-
if (root == null) return 0;
14-
return helper(0, root);
13+
return dfs(root, 0);
1514
}
1615

17-
public int helper(int tmp, TreeNode root) {
18-
tmp = tmp * 10 + root.val;
19-
if (root.left == null && root.right == null) {
20-
return tmp;
21-
} else {
22-
int sum = 0;
23-
if (root.left != null) sum += helper(tmp, root.left);
24-
if (root.right != null) sum += helper(tmp, root.right);
25-
return sum;
16+
private int dfs(TreeNode cur, int pathSum) {
17+
if (cur == null) return pathSum;
18+
pathSum = pathSum * 10 + cur.val;
19+
if (cur.left == null && cur.right == null) return pathSum;
20+
21+
// 如果不是叶子节点,则还不能返回当前的 pathSum,而是将 pathSum 作为参数继续计算
22+
int res = 0;
23+
if (cur.left != null) {
24+
res += dfs(cur.left, pathSum);
2625
}
26+
if (cur.right != null) {
27+
res += dfs(cur.right, pathSum);
28+
}
29+
return res;
2730
}
2831
}
2932

Java/leetcode 144.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,25 @@ public void helper(TreeNode root, List<Integer> res) {
2323
}
2424
}
2525

26-
// Solution2: HashMap T: O(n) S: O(n)
26+
// Solutuon2: iterative T: O(n) S: O(n)
27+
class Solution {
28+
public List<Integer> preorderTraversal(TreeNode root) {
29+
List<Integer> res = new ArrayList<>();
30+
Stack<TreeNode> stack = new Stack<>();
31+
if (root == null) return res;
32+
stack.push(root);
33+
while (!stack.isEmpty()) {
34+
TreeNode cur = stack.pop();
35+
if (cur == null) continue;
36+
res.add(cur.val);
37+
stack.push(cur.right);
38+
stack.push(cur.left);
39+
}
40+
return res;
41+
}
42+
}
43+
44+
// Solution3: HashMap T: O(n) S: O(n)
2745
class Solution {
2846
public List<Integer> preorderTraversal(TreeNode root) {
2947
List<Integer> res = new ArrayList<Integer>();
@@ -56,20 +74,4 @@ public List<Integer> preorderTraversal(TreeNode root) {
5674
}
5775
}
5876

59-
//Solutuon3: iterative T: O(n) S: O(n)
60-
class Solution {
61-
public List<Integer> preorderTraversal(TreeNode root) {
62-
List<Integer> res = new LinkedList<>();
63-
if(root == null) return res;
64-
Stack<TreeNode> stack = new Stack<>();
65-
stack.push(root);
66-
while(!stack.isEmpty()) {
67-
TreeNode cur = stack.pop();
68-
res.add(cur.val);
69-
if(cur.right != null) stack.push(cur.right);
70-
if(cur.left != null) stack.push(cur.left);
71-
}
72-
return res;
73-
}
74-
}
7577

Java/leetcode 216.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
class Solution {
22
public List<List<Integer>> combinationSum3(int k, int n) {
3-
List<List<Integer>> list = new ArrayList<>();
4-
backtrack(list, new ArrayList<>(), k, n, 1);
5-
return list;
3+
List<List<Integer>> res = new ArrayList<>();
4+
backtrack(res, new ArrayList<>(), 1, k, n);
5+
return res;
66
}
77

8-
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int k, int n, int start) {
9-
if(tempList.size() == k && n == 0)
10-
list.add(new ArrayList<>(tempList));
11-
12-
if(tempList.size() == k) return;
13-
14-
else {
15-
for(int i = start; i <= 9; i++) {
16-
tempList.add(i);
17-
backtrack(list, tempList, k, n - i, i + 1);
18-
tempList.remove(tempList.size() - 1);
19-
}
8+
private void backtrack(List<List<Integer>> res, List<Integer> curPath,
9+
int start, int k, int remain) {
10+
if (curPath.size() == k && remain == 0) {
11+
res.add(new ArrayList<>(curPath));
12+
return;
13+
}
14+
for (int i = start; i <= 9; i++) {
15+
if (curPath.size() >= k) break;
16+
if (remain - i < 0) break;
17+
curPath.add(i);
18+
backtrack(res, curPath, i+1, k, remain - i);
19+
curPath.remove(curPath.size() - 1);
2020
}
2121
}
2222
}

Java/leetcode 226.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,37 @@
77
* TreeNode(int x) { val = x; }
88
* }
99
*/
10-
// Solution1: recursion T: O(n) S: O(n) worst
10+
// Solution1: recursion pre-order T: O(n) S: O(n)
1111
class Solution {
1212
public TreeNode invertTree(TreeNode root) {
13-
if(root == null) return null;
14-
TreeNode tmp = root.left;
15-
root.left = invertTree(root.right);
16-
root.right = invertTree(tmp);
13+
dfs(root);
1714
return root;
1815
}
16+
17+
private void dfs(TreeNode cur) {
18+
if (cur == null) return;
19+
TreeNode tmp = cur.left;
20+
cur.left = cur.right;
21+
cur.right = tmp;
22+
dfs(cur.left);
23+
dfs(cur.right);
24+
}
1925
}
2026

21-
// Solution2: iterative T: O(n) S: O(n) (wide = n/2)
27+
// Solution2: iterative pre-order T: O(n) S: O(n)
2228
class Solution {
2329
public TreeNode invertTree(TreeNode root) {
24-
if (root == null) return null;
25-
Queue<TreeNode> queue = new LinkedList<>();
26-
queue.offer(root);
27-
while (!queue.isEmpty()) {
28-
TreeNode cur = queue.poll();
30+
if (root == null) return root;
31+
Stack<TreeNode> stack = new Stack<>();
32+
stack.push(root);
33+
while (!stack.isEmpty()) {
34+
TreeNode cur = stack.pop();
35+
if (cur == null) continue;
2936
TreeNode tmp = cur.left;
3037
cur.left = cur.right;
3138
cur.right = tmp;
32-
if (cur.left != null) queue.offer(cur.left);
33-
if (cur.right != null) queue.offer(cur.right);
39+
stack.push(cur.left);
40+
stack.push(cur.right);
3441
}
3542
return root;
3643
}

0 commit comments

Comments
 (0)