Skip to content

Commit eecd3aa

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

7 files changed

+339
-2
lines changed

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,48 @@
642642
#### Bipartite
643643
* [785. Is Graph Bipartite?](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/785.%20Is%20Graph%20Bipartite?.md)
644644
* [886. Possible Bipartition](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/886.%20Possible%20Bipartition.md)
645+
#### Cycle+Union-find
646+
* [684. Redundant Connection](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/684.%20Redundant%20Connection.md)
647+
* [685. Redundant Connection II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/685.%20Redundant%20Connection%20II.md)
648+
* [787. Cheapest Flights Within K Stops](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/787.%20Cheapest%20Flights%20Within%20K%20Stops.md)
649+
#### Shortest Path
650+
* [743. Network Delay Time](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/743.%20Network%20Delay%20Time.md)
651+
#### BFS
652+
* [847. Shortest Path Visiting All Nodes](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/847.%20Shortest%20Path%20Visiting%20All%20Nodes.md)
653+
* [815. Bus Routes](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/815.%20Bus%20Routes.md)
654+
* [864. Shortest Path to Get All Keys](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/864.%20Shortest%20Path%20to%20Get%20All%20Keys.md)
655+
* [924. Minimize Malware Spread](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/924.%20Minimize%20Malware%20Spread.md)
656+
#### Hamilton Path / DP
657+
* [943. Find the Shortest Superstring](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/943.%20Find%20the%20Shortest%20Superstring.md)
658+
* [980. Unique Paths III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/980.%20Unique%20Paths%20III.md)
659+
* [996. Number of Squareful Arrays](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/996.%20Number%20of%20Squareful%20Arrays.md)
660+
#### Union-find + connected component
661+
* [959. Regions Cut By Slashes](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/959.%20Regions%20Cut%20By%20Slashes.md)
662+
663+
### Tree
664+
#### Traversal
665+
* [94. Binary Tree Inorder Traversal](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/94.%20Binary%20Tree%20Inorder%20Traversal.md)
666+
* [589. N-ary Tree Preorder Traversal](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/589.%20N-ary%20Tree%20Preorder%20Traversal.md)
667+
* [590. N-ary Tree Postorder Traversal](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/590.%20N-ary%20Tree%20Postorder%20Traversal.md)
668+
* [100. Same Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/100.%20Same%20Tree.md)
669+
* [101. Symmetric Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/101.%20Symmetric%20Tree.md)
670+
* [104. Maximum Depth of Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/104.%20Maximum%20Depth%20of%20Binary%20Tree.md)
671+
* [110. Balanced Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/110.%20Balanced%20Binary%20Tree.md)
672+
* [111. Minimum Depth of Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/111.%20Minimum%20Depth%20of%20Binary%20Tree.md)
673+
* [572. Subtree of Another Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/572.%20Subtree%20of%20Another%20Tree.md)
674+
* [965. Univalued Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/965.%20Univalued%20Binary%20Tree.md)
675+
#### Collecting Node
676+
* [102. Binary Tree Level Order Traversal](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/102.%20Binary%20Tree%20Level%20Order%20Traversal.md)
677+
* [107. Binary Tree Level Order Traversal II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/107.%20Binary%20Tree%20Level%20Order%20Traversal%20II.md)
678+
* [429. N-ary Tree Level Order Traversal](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/429.%20N-ary%20Tree%20Level%20Order%20Traversal.md)
679+
* [987. Vertical Order Traversal of a Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/987.%20Vertical%20Order%20Traversal%20of%20a%20Binary%20Tree.md)
680+
* [814. Binary Tree Pruning](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/814.%20Binary%20Tree%20Pruning.md)
681+
* [669. Trim a Binary Search Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/669.%20Trim%20a%20Binary%20Search%20Tree.md)
682+
* [112. Path Sum](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/112.%20Path%20Sum.md)
683+
* [113. Path Sum II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/113.%20Path%20Sum%20II.md)
684+
* [437. Path Sum III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/437.%20Path%20Sum%20III.md)
685+
686+
645687

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

leetcode/112. Path Sum.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,33 @@ class Solution {
8989
}
9090
}
9191
}
92-
```
92+
```
93+
94+
### Third Time
95+
* Method 1: DFS
96+
```Java
97+
/**
98+
* Definition for a binary tree node.
99+
* public class TreeNode {
100+
* int val;
101+
* TreeNode left;
102+
* TreeNode right;
103+
* TreeNode(int x) { val = x; }
104+
* }
105+
*/
106+
class Solution {
107+
public boolean hasPathSum(TreeNode root, int sum) {
108+
if(root == null) return false;
109+
return dfs(root, 0, sum);
110+
}
111+
private boolean dfs(TreeNode node, int cur, int sum){
112+
if(node.left == null && node.right == null){
113+
return cur + node.val == sum;
114+
}else{
115+
if(node.left != null && dfs(node.left, cur + node.val, sum)) return true;
116+
if(node.right != null && dfs(node.right, cur + node.val, sum)) return true;
117+
return false;
118+
}
119+
}
120+
}
121+
```

leetcode/113. Path Sum II.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,40 @@ class Solution {
105105
temp.remove(temp.size() - 1);
106106
}
107107
}
108-
```
108+
```
109+
110+
### Third Time
111+
* Method 1: dfs
112+
```Java
113+
/**
114+
* Definition for a binary tree node.
115+
* public class TreeNode {
116+
* int val;
117+
* TreeNode left;
118+
* TreeNode right;
119+
* TreeNode(int x) { val = x; }
120+
* }
121+
*/
122+
class Solution {
123+
private List<List<Integer>> result;
124+
public List<List<Integer>> pathSum(TreeNode root, int sum) {
125+
result = new ArrayList<>();
126+
if(root == null) return result;
127+
dfs(root, new ArrayList<>(), 0, sum);
128+
return result;
129+
}
130+
private void dfs(TreeNode node, List<Integer> temp, int cur, int sum){
131+
if(node == null){
132+
if(cur == sum) result.add(new ArrayList<Integer>(temp));
133+
}else{
134+
temp.add(node.val);
135+
if(node.left == null && node.right == null) dfs(node.left, temp, cur + node.val, sum);
136+
else{
137+
if(node.left != null) dfs(node.left, temp, cur + node.val, sum);
138+
if(node.right != null) dfs(node.right, temp, cur + node.val, sum);
139+
}
140+
temp.remove(temp.size() - 1);
141+
}
142+
}
143+
}
144+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## 429. N-ary Tree Level Order Traversal
2+
3+
### Question:
4+
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
5+
6+
```
7+
We should return its level order traversal:
8+
[
9+
[1],
10+
[3,2,4],
11+
[5,6]
12+
]
13+
```
14+
15+
Note:
16+
1. The depth of the tree is at most 1000.
17+
2. The total number of nodes is at most 5000.
18+
19+
### Solution:
20+
* Method 1: bfs
21+
```Java
22+
/*
23+
// Definition for a Node.
24+
class Node {
25+
public int val;
26+
public List<Node> children;
27+
28+
public Node() {}
29+
30+
public Node(int _val,List<Node> _children) {
31+
val = _val;
32+
children = _children;
33+
}
34+
};
35+
*/
36+
class Solution {
37+
public List<List<Integer>> levelOrder(Node root) {
38+
List<List<Integer>> result = new ArrayList<>();
39+
if(root == null) return result;
40+
Queue<Node> q = new LinkedList<>();
41+
q.offer(root);
42+
while(!q.isEmpty()){
43+
List<Integer> temp = new ArrayList<>();
44+
int size = q.size();
45+
for(int i = 0; i < size; i++){
46+
Node node = q.poll();
47+
temp.add(node.val);
48+
for(Node n : node.children){
49+
q.offer(n);
50+
}
51+
}
52+
result.add(temp);
53+
}
54+
return result;
55+
}
56+
}
57+
```
58+

leetcode/437. Path Sum III.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## 437. Path Sum III
2+
3+
### Question:
4+
You are given a binary tree in which each node contains an integer value.
5+
6+
Find the number of paths that sum to a given value.
7+
8+
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
9+
10+
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
11+
12+
```
13+
Example:
14+
15+
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
16+
17+
10
18+
/ \
19+
5 -3
20+
/ \ \
21+
3 2 11
22+
/ \ \
23+
3 -2 1
24+
25+
Return 3. The paths that sum to 8 are:
26+
27+
1. 5 -> 3
28+
2. 5 -> 2 -> 1
29+
3. -3 -> 11
30+
```
31+
32+
33+
### Solution:
34+
* Method 1: dfs + bfs
35+
```Java
36+
/**
37+
* Definition for a binary tree node.
38+
* public class TreeNode {
39+
* int val;
40+
* TreeNode left;
41+
* TreeNode right;
42+
* TreeNode(int x) { val = x; }
43+
* }
44+
*/
45+
class Solution {
46+
private int result;
47+
public int pathSum(TreeNode root, int sum) {
48+
if(root == null) return 0;
49+
Queue<TreeNode> q = new LinkedList<>();
50+
q.offer(root);
51+
while(!q.isEmpty()){
52+
TreeNode node = q.poll();
53+
dfs(node, 0, sum);
54+
if(node.left != null) q.offer(node.left);
55+
if(node.right != null) q.offer(node.right);
56+
}
57+
return result;
58+
}
59+
private void dfs(TreeNode node, int cur, int sum){
60+
if(node == null) return;
61+
if(cur + node.val == sum) result++;
62+
dfs(node.left, cur + node.val, sum);
63+
dfs(node.right, cur + node.val, sum);
64+
}
65+
}
66+
```
67+

leetcode/872. Leaf-Similar Trees.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## 872. Leaf-Similar Trees
2+
3+
### Question:
4+
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence.
5+
6+
```
7+
For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).
8+
9+
Two binary trees are considered leaf-similar if their leaf value sequence is the same.
10+
11+
Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.
12+
```
13+
14+
Note:
15+
* Both of the given trees will have between 1 and 100 nodes.
16+
17+
### Solution:
18+
* Method 1: dfs
19+
```Java
20+
/**
21+
* Definition for a binary tree node.
22+
* public class TreeNode {
23+
* int val;
24+
* TreeNode left;
25+
* TreeNode right;
26+
* TreeNode(int x) { val = x; }
27+
* }
28+
*/
29+
class Solution {
30+
private List<Integer> leaves1;
31+
private List<Integer> leaves2;
32+
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
33+
this.leaves1 = new ArrayList<>();
34+
this.leaves2 = new ArrayList<>();
35+
dfs(root1, leaves1);
36+
dfs(root2, leaves2);
37+
if(leaves1.size() != leaves2.size()) return false;
38+
for(int i = 0; i < leaves1.size(); i++){
39+
if(leaves1.get(i) != leaves2.get(i)) return false;
40+
}
41+
return true;
42+
}
43+
private void dfs(TreeNode node, List<Integer> list){
44+
if(node.left == null && node.right == null){
45+
list.add(node.val);
46+
return;
47+
}
48+
if(node.left != null) dfs(node.left, list);
49+
if(node.right != null) dfs(node.right, list);
50+
}
51+
}
52+
```
53+
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## 965. Univalued Binary Tree
2+
3+
### Question:
4+
A binary tree is univalued if every node in the tree has the same value.
5+
6+
Return true if and only if the given tree is univalued.
7+
8+
```
9+
Example 1:
10+
11+
Input: [1,1,1,1,1,null,1]
12+
Output: true
13+
14+
Example 2:
15+
16+
Input: [2,2,2,5,2]
17+
Output: false
18+
```
19+
20+
21+
Note:
22+
1. The number of nodes in the given tree will be in the range [1, 100].
23+
2. Each node's value will be an integer in the range [0, 99].
24+
25+
### Solution:
26+
* Method 1: dfs
27+
```Java
28+
/**
29+
* Definition for a binary tree node.
30+
* public class TreeNode {
31+
* int val;
32+
* TreeNode left;
33+
* TreeNode right;
34+
* TreeNode(int x) { val = x; }
35+
* }
36+
*/
37+
class Solution {
38+
private int pre;
39+
public boolean isUnivalTree(TreeNode root) {
40+
pre = root.val;
41+
return check(root.left) && check(root.right);
42+
}
43+
private boolean check(TreeNode node){
44+
if(node == null) return true;
45+
else if(pre != node.val) return false;
46+
else{
47+
return check(node.left) && check(node.right);
48+
}
49+
}
50+
}
51+
```
52+

0 commit comments

Comments
 (0)