Skip to content

Commit 5d27798

Browse files
committed
[Function add]
1. Add leetcode solutions with tag tree.
1 parent 7bf0c26 commit 5d27798

5 files changed

+228
-28
lines changed

README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,20 @@
682682
* [112. Path Sum](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/112.%20Path%20Sum.md)
683683
* [113. Path Sum II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/113.%20Path%20Sum%20II.md)
684684
* [437. Path Sum III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/437.%20Path%20Sum%20III.md)
685-
686-
685+
#### Use both nodes, return one.
686+
* [124. Binary Tree Maximum Path Sum](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/124.%20Binary%20Tree%20Maximum%20Path%20Sum.md)
687+
* [543. Diameter of Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/543.%20Diameter%20of%20Binary%20Tree.md)
688+
* [687. Longest Univalue Path](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/687.%20Longest%20Univalue%20Path.md)
689+
* [129. Sum Root to Leaf Numbers](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/129.%20Sum%20Root%20to%20Leaf%20Numbers.md)
690+
* [257. Binary Tree Paths](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/257.%20Binary%20Tree%20Paths.md)
691+
* [235. Lowest Common Ancestor of a Binary Search Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/235.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree.md)
692+
* [236. Lowest Common Ancestor of a Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/236.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree.md)
693+
* [297. Serialize and Deserialize Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/297.%20Serialize%20and%20Deserialize%20Binary%20Tree.md)
694+
* [449. Serialize and Deserialize BST](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/449.%20Serialize%20and%20Deserialize%20BST.md)
695+
* [508. Most Frequent Subtree Sum](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/508.%20Most%20Frequent%20Subtree%20Sum.md)
696+
* [968. Binary Tree Cameras](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/968.%20Binary%20Tree%20Cameras.md)
697+
* [337. House Robber III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/337.%20House%20Robber%20III.md)
698+
* [979. Distribute Coins in Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/979.%20Distribute%20Coins%20in%20Binary%20Tree.md)
687699

688700
## Algorithm(4th_Edition)
689701
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.

leetcode/236. Lowest Common Ancestor of a Binary Tree.md

+25
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,28 @@ class Solution {
181181
}
182182
}
183183
```
184+
185+
### Third Time
186+
* Method 1: Divide and Conquer
187+
```Java
188+
/**
189+
* Definition for a binary tree node.
190+
* public class TreeNode {
191+
* int val;
192+
* TreeNode left;
193+
* TreeNode right;
194+
* TreeNode(int x) { val = x; }
195+
* }
196+
*/
197+
class Solution {
198+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
199+
if(root == p || root == q || root == null) return root;
200+
TreeNode left = lowestCommonAncestor(root.left, p, q);
201+
TreeNode right = lowestCommonAncestor(root.right, p, q);
202+
if(left != null && right != null) return root;
203+
else if(left != null) return left;
204+
else if(right != null) return right;
205+
return null;
206+
}
207+
}
208+
```

leetcode/337. House Robber III.md

+56-26
Original file line numberDiff line numberDiff line change
@@ -79,29 +79,59 @@ class Solution {
7979
* 如何求出到当前节点的最大值。
8080
* 我们需要两个条件,到子结点的最大值,孙结点的最大值 + 当前值。
8181

82-
```Java
83-
/**
84-
* Definition for a binary tree node.
85-
* public class TreeNode {
86-
* int val;
87-
* TreeNode left;
88-
* TreeNode right;
89-
* TreeNode(int x) { val = x; }
90-
* }
91-
*/
92-
class Solution {
93-
public int rob(TreeNode root) {
94-
return dfs(root)[0];
95-
}
96-
private int[] dfs(TreeNode root){
97-
int[] max = {0, 0}; //max[0]: 到当前节点的最大值; max[1]: 到子结点的最大值
98-
if(root != null){
99-
int[] maxLeft = dfs(root.left);
100-
int[] maxRight = dfs(root.right);
101-
max[1] = maxLeft[0] + maxRight[0]; //子结点的最大值
102-
max[0] = Math.max(maxLeft[1] + maxRight[1] + root.val, max[1]); //当前的最大值为max(孙结点的最大值 + 当前值, 子结点最大值)。
103-
}
104-
return max;
105-
}
106-
}
107-
```
82+
```Java
83+
/**
84+
* Definition for a binary tree node.
85+
* public class TreeNode {
86+
* int val;
87+
* TreeNode left;
88+
* TreeNode right;
89+
* TreeNode(int x) { val = x; }
90+
* }
91+
*/
92+
class Solution {
93+
public int rob(TreeNode root) {
94+
return dfs(root)[0];
95+
}
96+
private int[] dfs(TreeNode root){
97+
int[] max = {0, 0}; //max[0]: 到当前节点的最大值; max[1]: 到子结点的最大值
98+
if(root != null){
99+
int[] maxLeft = dfs(root.left);
100+
int[] maxRight = dfs(root.right);
101+
max[1] = maxLeft[0] + maxRight[0]; //子结点的最大值
102+
max[0] = Math.max(maxLeft[1] + maxRight[1] + root.val, max[1]); //当前的最大值为max(孙结点的最大值 + 当前值, 子结点最大值)。
103+
}
104+
return max;
105+
}
106+
}
107+
```
108+
109+
### Second Time
110+
* Method 1: DFS + DP
111+
```Java
112+
/**
113+
* Definition for a binary tree node.
114+
* public class TreeNode {
115+
* int val;
116+
* TreeNode left;
117+
* TreeNode right;
118+
* TreeNode(int x) { val = x; }
119+
* }
120+
*/
121+
class Solution {
122+
public int rob(TreeNode root) {
123+
return dfs(root)[0];
124+
}
125+
private int[] dfs(TreeNode node){
126+
if(node == null) return new int[]{0, 0};
127+
else{
128+
int[] left = dfs(node.left);
129+
int[] right = dfs(node.right);
130+
int[] result = new int[2];
131+
result[1] = left[0] + right[0];
132+
result[0] = Math.max(result[1], node.val + left[1] + right[1]);
133+
return result;
134+
}
135+
}
136+
}
137+
```

leetcode/968. Binary Tree Cameras.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## 968. Binary Tree Cameras
2+
3+
### Question:
4+
Given a binary tree, we install cameras on the nodes of the tree.
5+
Each camera at a node can monitor its parent, itself, and its immediate children.
6+
Calculate the minimum number of cameras needed to monitor all nodes of the tree.
7+
8+
```
9+
Example 1:
10+
11+
Input: [0,0,null,0,0]
12+
Output: 1
13+
Explanation: One camera is enough to monitor all nodes if placed as shown.
14+
15+
Example 2:
16+
17+
Input: [0,0,null,0,null,0,null,null,0]
18+
Output: 2
19+
Explanation: At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
20+
```
21+
22+
Note:
23+
1. The number of nodes in the given tree will be in the range [1, 1000].
24+
2. Every node has value 0.
25+
26+
### Solution:
27+
* Method 1: dfs + Greedy
28+
* I didn't get this question by myself.
29+
```Java
30+
/**
31+
* Definition for a binary tree node.
32+
* public class TreeNode {
33+
* int val;
34+
* TreeNode left;
35+
* TreeNode right;
36+
* TreeNode(int x) { val = x; }
37+
* }
38+
*/
39+
class Solution {
40+
private static final int NULL = 0;
41+
private static final int COVER = 1;
42+
private static final int CAMERA = 2;
43+
private int res;
44+
public int minCameraCover(TreeNode root) {
45+
if(dfs(root) == NULL) res++;
46+
return res;
47+
}
48+
private int dfs(TreeNode node){
49+
if(node == null) return COVER;
50+
int left = dfs(node.left);
51+
int right = dfs(node.right);
52+
if(left == NULL || right == NULL){
53+
res++;
54+
return CAMERA;
55+
}
56+
if(left == CAMERA || right == CAMERA) return COVER;
57+
return NULL;
58+
}
59+
}
60+
```
61+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
## 979. Distribute Coins in Binary Tree
2+
3+
### Question:
4+
Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total.
5+
6+
In one move, we may choose two adjacent nodes and move one coin from one node to another. (The move may be from parent to child, or from child to parent.)
7+
8+
Return the number of moves required to make every node have exactly one coin.
9+
10+
```
11+
Example 1:
12+
13+
Input: [3,0,0]
14+
Output: 2
15+
Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.
16+
17+
Example 2:
18+
19+
Input: [0,3,0]
20+
Output: 3
21+
Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.
22+
23+
Example 3:
24+
25+
Input: [1,0,2]
26+
Output: 2
27+
28+
Example 4:
29+
30+
Input: [1,0,0,null,3]
31+
Output: 4
32+
```
33+
34+
Note:
35+
1. 1<= N <= 100
36+
2. 0 <= node.val <= N
37+
38+
39+
40+
### Solution:
41+
* Method 1: Recursion
42+
```Java
43+
/**
44+
* Definition for a binary tree node.
45+
* public class TreeNode {
46+
* int val;
47+
* TreeNode left;
48+
* TreeNode right;
49+
* TreeNode(int x) { val = x; }
50+
* }
51+
*/
52+
class Solution {
53+
private int res;
54+
public int distributeCoins(TreeNode root) {
55+
balance(root);
56+
return res;
57+
}
58+
private int balance(TreeNode node){
59+
if(node == null) return 0;
60+
else{
61+
int left = balance(node.left);
62+
int right = balance(node.right);
63+
res += Math.abs(left) + Math.abs(right);
64+
return node.val - 1 + left + right;
65+
}
66+
}
67+
}
68+
```
69+
70+
### Reference
71+
1. [花花酱 LeetCode 979. Distribute Coins in Binary Tree](https://zxi.mytechroad.com/blog/tree/leetcode-979-distribute-coins-in-binary-tree/)
72+

0 commit comments

Comments
 (0)