Skip to content

Commit f698861

Browse files
committed
[Function add]
1. Add leetcode solutions with tag tree.
1 parent eecd3aa commit f698861

5 files changed

+216
-2
lines changed

leetcode/124. Binary Tree Maximum Path Sum.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,32 @@ class Solution {
5858
return Math.max(cur, Math.max(cur + leftSum, cur + rightSum)); //返回到上一层的可能性有三种, 只返回当前结点,左结点往上, 右结点往上。
5959
}
6060
}
61-
```
61+
```
62+
63+
### Second Time
64+
* Method 1: Recursion
65+
```Java
66+
/**
67+
* Definition for a binary tree node.
68+
* public class TreeNode {
69+
* int val;
70+
* TreeNode left;
71+
* TreeNode right;
72+
* TreeNode(int x) { val = x; }
73+
* }
74+
*/
75+
class Solution {
76+
private int max = Integer.MIN_VALUE;
77+
public int maxPathSum(TreeNode root) {
78+
return Math.max(max(root), this.max);
79+
}
80+
private int max(TreeNode node){
81+
if(node == null) return 0;
82+
int left = Math.max(max(node.left), 0);
83+
int right = Math.max(max(node.right), 0);
84+
int res = Math.max(node.val, node.val + left + right);
85+
this.max = Math.max(max, res);
86+
return Math.max(node.val, Math.max(node.val + left, node.val + right));
87+
}
88+
}
89+
```

leetcode/129. Sum Root to Leaf Numbers.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,39 @@ class Solution {
106106
}
107107
}
108108
}
109-
```
109+
```
110+
111+
### Third Time
112+
* Method 1: dfs
113+
```Java
114+
/**
115+
* Definition for a binary tree node.
116+
* public class TreeNode {
117+
* int val;
118+
* TreeNode left;
119+
* TreeNode right;
120+
* TreeNode(int x) { val = x; }
121+
* }
122+
*/
123+
class Solution {
124+
private List<Integer> path;
125+
public int sumNumbers(TreeNode root) {
126+
this.path = new ArrayList<>();
127+
dfs(root, 0);
128+
int res = 0;
129+
for(Integer n : path)
130+
res += n;
131+
return res;
132+
}
133+
private void dfs(TreeNode node, int cur){
134+
if(node == null){
135+
this.path.add(cur);
136+
return;
137+
}
138+
cur = cur * 10 + node.val;
139+
if(node.left == null && node.right == null) dfs(null, cur);
140+
if(node.left != null) dfs(node.left, cur);
141+
if(node.right != null) dfs(node.right, cur);
142+
}
143+
}
144+
```

leetcode/257. Binary Tree Paths.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,34 @@ class Solution {
102102
}
103103
}
104104
```
105+
106+
### Third Time
107+
* Method 1: dfs
108+
```Java
109+
/**
110+
* Definition for a binary tree node.
111+
* public class TreeNode {
112+
* int val;
113+
* TreeNode left;
114+
* TreeNode right;
115+
* TreeNode(int x) { val = x; }
116+
* }
117+
*/
118+
class Solution {
119+
private List<String> result;
120+
public List<String> binaryTreePaths(TreeNode root) {
121+
this.result = new ArrayList<>();
122+
if(root == null) return result;
123+
if(root.left == null && root.right == null) result.add(root.val + "");
124+
if(root.left != null) dfs(root.left, root.val + "");
125+
if(root.right != null) dfs(root.right, root.val + "");
126+
return result;
127+
}
128+
private void dfs(TreeNode node, String s){
129+
String cur = s + "->" + node.val;
130+
if(node.left == null && node.right == null) result.add(cur);
131+
if(node.left != null) dfs(node.left, cur);
132+
if(node.right != null) dfs(node.right, cur);
133+
}
134+
}
135+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## 543. Diameter of Binary Tree
2+
3+
### Question:
4+
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
5+
6+
```
7+
Example:
8+
Given a binary tree
9+
10+
1
11+
/ \
12+
2 3
13+
/ \
14+
4 5
15+
16+
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
17+
```
18+
19+
Note: The length of path between two nodes is represented by the number of edges between them.
20+
21+
### Solution:
22+
* Method 1: Divide and conquer(Recursion).
23+
```Java
24+
/**
25+
* Definition for a binary tree node.
26+
* public class TreeNode {
27+
* int val;
28+
* TreeNode left;
29+
* TreeNode right;
30+
* TreeNode(int x) { val = x; }
31+
* }
32+
*/
33+
class Solution {
34+
private int result;
35+
public int diameterOfBinaryTree(TreeNode root) {
36+
if(root == null) return 0;
37+
maxPath(root);
38+
return this.result - 1;
39+
}
40+
private int maxPath(TreeNode root){
41+
if(root == null) return 0;
42+
int left = maxPath(root.left);
43+
int right = maxPath(root.right);
44+
this.result = Math.max(result, left + right + 1);
45+
return Math.max(left, right) + 1;
46+
}
47+
}
48+
```
49+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## 687. Longest Univalue Path
2+
3+
### Question:
4+
Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.
5+
6+
Note: The length of path between two nodes is represented by the number of edges between them.
7+
8+
```
9+
Example 1:
10+
11+
Input:
12+
13+
5
14+
/ \
15+
4 5
16+
/ \ \
17+
1 1 5
18+
19+
Output:
20+
21+
2
22+
23+
Example 2:
24+
25+
Input:
26+
27+
1
28+
/ \
29+
4 5
30+
/ \ \
31+
4 4 5
32+
33+
Output:
34+
35+
2
36+
```
37+
38+
Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.
39+
40+
### Solution:
41+
* Method 1: Divide and conquer(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 result;
54+
public int longestUnivaluePath(TreeNode root) {
55+
if(root == null) return 0;
56+
getPath(root);
57+
return this.result - 1;
58+
}
59+
private int getPath(TreeNode node){
60+
if(node == null) return 0;
61+
int left = getPath(node.left);
62+
int right = getPath(node.right);
63+
int res = 1 + ((node.left == null || node.left.val == node.val) ? left: 0)
64+
+ ((node.right == null || node.right.val == node.val) ? right: 0);
65+
this.result = Math.max(result, res);
66+
return Math.max((node.left == null || node.left.val == node.val) ? left: 0,
67+
(node.right == null || node.right.val == node.val) ? right: 0) + 1;
68+
}
69+
}
70+
```
71+

0 commit comments

Comments
 (0)