Skip to content

Commit 1eefca6

Browse files
committed
[Function add]
1. Add leetcode solutions with tag tree.
1 parent 88aa96d commit 1eefca6

File tree

3 files changed

+277
-0
lines changed

3 files changed

+277
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
## 669. Trim a Binary Search Tree
2+
3+
### Question:
4+
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.
5+
6+
```
7+
Example 1:
8+
9+
Input:
10+
1
11+
/ \
12+
0 2
13+
14+
L = 1
15+
R = 2
16+
17+
Output:
18+
1
19+
\
20+
2
21+
22+
Example 2:
23+
24+
Input:
25+
3
26+
/ \
27+
0 4
28+
\
29+
2
30+
/
31+
1
32+
33+
L = 1
34+
R = 3
35+
36+
Output:
37+
3
38+
/
39+
2
40+
/
41+
1
42+
```
43+
44+
45+
### Solution:
46+
* Method 1: divide and conquer (O(V + E), time complexity of dfs )
47+
* divide: we do the trim for left and right node.
48+
* if current node is within the range, we just return current node.
49+
* if current node needs to be trimmed, we need to append the right node to the very right of left subtree and return the left node of current node.
50+
```Java
51+
/**
52+
* Definition for a binary tree node.
53+
* public class TreeNode {
54+
* int val;
55+
* TreeNode left;
56+
* TreeNode right;
57+
* TreeNode(int x) { val = x; }
58+
* }
59+
*/
60+
class Solution {
61+
public TreeNode trimBST(TreeNode root, int L, int R) {
62+
if(root == null) return root;
63+
TreeNode left = trimBST(root.left, L, R);
64+
root.left = left;
65+
TreeNode right = trimBST(root.right, L, R);
66+
root.right = right;
67+
if(root.val < L || root.val > R){
68+
if(root.left == null) return root.right;
69+
else{
70+
TreeNode node = root.left;
71+
while(node.right != null){
72+
node = node.right;
73+
}
74+
node.right = root.right;
75+
root.right = null;
76+
return root.left;
77+
}
78+
}
79+
return root;
80+
}
81+
}
82+
```
83+

leetcode/814. Binary Tree Pruning.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
## 814. Binary Tree Pruning
2+
3+
### Question:
4+
We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1.
5+
6+
Return the same tree where every subtree (of the given tree) not containing a 1 has been removed.
7+
8+
(Recall that the subtree of a node X is X, plus every node that is a descendant of X.)
9+
10+
```
11+
Example 1:
12+
Input: [1,null,0,0,1]
13+
Output: [1,null,0,null,1]
14+
15+
Explanation:
16+
Only the red nodes satisfy the property "every subtree not containing a 1".
17+
The diagram on the right represents the answer.
18+
19+
20+
Example 2:
21+
Input: [1,0,1,0,0,0,1]
22+
Output: [1,null,1,null,1]
23+
24+
Example 3:
25+
Input: [1,1,0,1,1,0,1,0]
26+
Output: [1,1,0,1,1,null,1]
27+
```
28+
29+
Note:
30+
1. The binary tree will have at most 100 nodes.
31+
2, The value of each node will only be 0 or 1.
32+
33+
34+
### Solution:
35+
* Method 1: recursion
36+
* For this question must make sure that for a single node, we need 2 flags for indicating left and right can be removed, we should initialize them as true.
37+
```Java
38+
/**
39+
* Definition for a binary tree node.
40+
* public class TreeNode {
41+
* int val;
42+
* TreeNode left;
43+
* TreeNode right;
44+
* TreeNode(int x) { val = x; }
45+
* }
46+
*/
47+
class Solution {
48+
public TreeNode pruneTree(TreeNode root) {
49+
if(root == null) return root;
50+
boolean left = true, right = true;
51+
if(root.left != null){
52+
left = pruneNode(root.left);
53+
if(left) root.left = null;
54+
}
55+
if(root.right != null){
56+
right = pruneNode(root.right);
57+
if(right) root.right = null;
58+
}
59+
if(left && right && root.val == 0){
60+
root = null;
61+
}
62+
return root;
63+
}
64+
private boolean pruneNode(TreeNode node){
65+
if(node.val == 0 && node.left == null && node.right == null) return true;
66+
else{
67+
boolean left = true, right = true;
68+
if(node.left != null){
69+
left = pruneNode(node.left);
70+
if(left) node.left = null;
71+
}
72+
if(node.right != null){
73+
right = pruneNode(node.right);
74+
if(right) node.right = null;
75+
}
76+
return (left && right) && node.val == 0;
77+
}
78+
}
79+
}
80+
```
81+
82+
* Method 2: Divide and conquer
83+
```Java
84+
/**
85+
* Definition for a binary tree node.
86+
* public class TreeNode {
87+
* int val;
88+
* TreeNode left;
89+
* TreeNode right;
90+
* TreeNode(int x) { val = x; }
91+
* }
92+
*/
93+
class Solution {
94+
public TreeNode pruneTree(TreeNode root){
95+
if(root == null) return root;
96+
TreeNode left = pruneTree(root.left);
97+
root.left = left;
98+
TreeNode right = pruneTree(root.right);
99+
root.right = right;
100+
if(root.val == 0 && root.left == null && root.right == null)
101+
root = null;
102+
return root;
103+
}
104+
}
105+
```
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
## 987. Vertical Order Traversal of a Binary Tree
2+
3+
### Question:
4+
Given a binary tree, return the vertical order traversal of its nodes values.
5+
6+
For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1).
7+
8+
Running a vertical line from X = -infinity to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y coordinates).
9+
10+
If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.
11+
12+
Return an list of non-empty reports in order of X coordinate. Every report will have a list of values of nodes.
13+
14+
```
15+
Example 1:
16+
17+
Input: [3,9,20,null,null,15,7]
18+
Output: [[9],[3,15],[20],[7]]
19+
Explanation:
20+
Without loss of generality, we can assume the root node is at position (0, 0):
21+
Then, the node with value 9 occurs at position (-1, -1);
22+
The nodes with values 3 and 15 occur at positions (0, 0) and (0, -2);
23+
The node with value 20 occurs at position (1, -1);
24+
The node with value 7 occurs at position (2, -2).
25+
26+
Example 2:
27+
28+
Input: [1,2,3,4,5,6,7]
29+
Output: [[4],[2],[1,5,6],[3],[7]]
30+
Explanation:
31+
The node with value 5 and the node with value 6 have the same position according to the given scheme.
32+
However, in the report "[1,5,6]", the node value of 5 comes first since 5 is smaller than 6.
33+
```
34+
35+
Note:
36+
1. The tree will have between 1 and 1000 nodes.
37+
2. Each node's value will be between 0 and 1000.
38+
39+
40+
41+
### Solution:
42+
* Method 1: DFS + TreeMap, dfs is for giving coordinators to nodes and TreeMap is for order.
43+
*Time complexity: dfs(O(Vertice + Edge)) + TreeMap(O(NlogN))
44+
```Java
45+
/**
46+
* Definition for a binary tree node.
47+
* public class TreeNode {
48+
* int val;
49+
* TreeNode left;
50+
* TreeNode right;
51+
* TreeNode(int x) { val = x; }
52+
* }
53+
*/
54+
class Solution {
55+
private TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map;
56+
public List<List<Integer>> verticalTraversal(TreeNode root) {
57+
map = new TreeMap<>();
58+
List<List<Integer>> result = new ArrayList<>();
59+
if(root == null) return result;
60+
dfs(0, 0, root);
61+
for(TreeMap<Integer, PriorityQueue<Integer>> value : map.values()){
62+
List<Integer> temp = new ArrayList<>();
63+
for(PriorityQueue<Integer> pq : value.values()){
64+
while(!pq.isEmpty()){
65+
temp.add(pq.poll());
66+
}
67+
}
68+
result.add(temp);
69+
}
70+
return result;
71+
}
72+
private void dfs(int x, int y, TreeNode node){
73+
TreeMap<Integer, PriorityQueue<Integer>> value = map.containsKey(x) ? map.get(x): new TreeMap<Integer, PriorityQueue<Integer>>(
74+
new Comparator<Integer>(){
75+
@Override
76+
public int compare(Integer n1, Integer n2){
77+
return n2 - n1;
78+
}
79+
}
80+
);
81+
PriorityQueue<Integer> pq = value.containsKey(y) ? value.get(y): new PriorityQueue<Integer>();
82+
pq.offer(node.val);
83+
value.put(y, pq);
84+
map.put(x, value);
85+
if(node.left != null) dfs(x - 1, y - 1, node.left);
86+
if(node.right != null) dfs(x + 1, y - 1, node.right);
87+
}
88+
}
89+
```

0 commit comments

Comments
 (0)