Skip to content

Commit 88aa96d

Browse files
author
Botao Xiao
committed
[Function add]: 1. Add leetcode solutions with tag graph and tree.
1 parent 92240e3 commit 88aa96d

12 files changed

+735
-10
lines changed

leetcode/100. Same Tree.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,27 @@ class Solution {
5757
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
5858
}
5959
}
60-
```
60+
```
61+
62+
### Second Time
63+
* Method 1: recursion
64+
```Java
65+
/**
66+
* Definition for a binary tree node.
67+
* public class TreeNode {
68+
* int val;
69+
* TreeNode left;
70+
* TreeNode right;
71+
* TreeNode(int x) { val = x; }
72+
* }
73+
*/
74+
class Solution {
75+
public boolean isSameTree(TreeNode p, TreeNode q) {
76+
if(p == null && q == null) return true;
77+
else if(p == null || q == null) return false;
78+
else{
79+
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
80+
}
81+
}
82+
}
83+
```

leetcode/101. Symmetric Tree.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ But the following [1,2,2,null,3,null,3] is not:
2121
3 3
2222
```
2323
Note:
24-
Bonus points if you could solve it both recursively and iteratively.
24+
Bonus points if you could solve it both recursively and iteratively.
2525

2626
### Thinking:
2727
* Method:
@@ -80,4 +80,31 @@ class Solution {
8080
}
8181
}
8282
}
83-
```
83+
```
84+
85+
### Third time
86+
* Method 1: recursion
87+
```Java
88+
/**
89+
* Definition for a binary tree node.
90+
* public class TreeNode {
91+
* int val;
92+
* TreeNode left;
93+
* TreeNode right;
94+
* TreeNode(int x) { val = x; }
95+
* }
96+
*/
97+
class Solution {
98+
public boolean isSymmetric(TreeNode root) {
99+
if(root == null) return true;
100+
return symmetric(root.left, root.right);
101+
}
102+
private boolean symmetric(TreeNode left, TreeNode right){
103+
if(left == null && right == null) return true;
104+
else if(left == null || right == null) return false;
105+
else{
106+
return left.val == right.val && symmetric(left.right, right.left) && symmetric(left.left, right.right);
107+
}
108+
}
109+
}
110+
```

leetcode/104. Maximum Depth of Binary Tree.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,24 @@ class Solution {
6767
return Math.max(getHeight(node.left), getHeight(node.right)) + 1;
6868
}
6969
}
70-
```
70+
```
71+
72+
### Third Time
73+
* Method 1
74+
```Java
75+
/**
76+
* Definition for a binary tree node.
77+
* public class TreeNode {
78+
* int val;
79+
* TreeNode left;
80+
* TreeNode right;
81+
* TreeNode(int x) { val = x; }
82+
* }
83+
*/
84+
class Solution {
85+
public int maxDepth(TreeNode root) {
86+
if(root == null) return 0;
87+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
88+
}
89+
}
90+
```

leetcode/110. Balanced Binary Tree.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,31 @@ class Solution {
8989
return Math.max(getHeight(node.left), getHeight(node.right)) + 1;
9090
}
9191
}
92-
```
92+
```
93+
94+
### Third Time
95+
* Method 1: Recursion
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 isBalanced(TreeNode root) {
108+
if(root == null) return true;
109+
int left = getHeight(root.left);
110+
int right = getHeight(root.right);
111+
if(Math.abs(left - right) > 1) return false;
112+
else return isBalanced(root.left) && isBalanced(root.right);
113+
}
114+
private int getHeight(TreeNode node){
115+
if(node == null) return 0;
116+
return Math.max(getHeight(node.left), getHeight(node.right)) + 1;
117+
}
118+
}
119+
```

leetcode/111. Minimum Depth of Binary Tree.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,27 @@ class Solution {
7676
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
7777
}
7878
}
79-
```
79+
```
80+
81+
### Third Time
82+
* Method 1: Recursion
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 int minDepth(TreeNode root) {
95+
if(root == null) return 0;
96+
if(root.left == null && root.right == null) return 1;
97+
else if(root.left != null && root.right != null) return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
98+
else if(root.left != null) return minDepth(root.left) + 1;
99+
else return minDepth(root.right) + 1;
100+
}
101+
}
102+
```
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## 572. Subtree of Another Tree
2+
3+
### Question
4+
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself.
5+
6+
```
7+
Example 1:
8+
Given tree s:
9+
10+
3
11+
/ \
12+
4 5
13+
/ \
14+
1 2
15+
Given tree t:
16+
4
17+
/ \
18+
1 2
19+
Return true, because t has the same structure and node values with a subtree of s.
20+
Example 2:
21+
Given tree s:
22+
23+
3
24+
/ \
25+
4 5
26+
/ \
27+
1 2
28+
/
29+
0
30+
Given tree t:
31+
4
32+
/ \
33+
1 2
34+
Return false.
35+
```
36+
37+
### Solution:
38+
* Method 1: Recursion
39+
```Java
40+
/**
41+
* Definition for a binary tree node.
42+
* public class TreeNode {
43+
* int val;
44+
* TreeNode left;
45+
* TreeNode right;
46+
* TreeNode(int x) { val = x; }
47+
* }
48+
*/
49+
class Solution {
50+
public boolean isSubtree(TreeNode s, TreeNode t) {
51+
if(s != null && t != null) return same(s, t) || (isSubtree(s.left, t) || isSubtree(s.right, t));
52+
else if(s != null || t != null) return false;
53+
else return true;
54+
}
55+
private boolean same(TreeNode s, TreeNode t){
56+
if(s != null && t != null) return s.val == t.val && same(s.left, t.left) && same(s.right, t.right);
57+
else if(s != null || t != null) return false;
58+
else return true;
59+
}
60+
}
61+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
## 589. N-ary Tree Preorder Traversal
2+
3+
### Question
4+
Given an n-ary tree, return the preorder traversal of its nodes' values.
5+
6+
For example, given a 3-ary tree:
7+
Return its preorder traversal as: [1,3,5,6,2,4].
8+
9+
Note:
10+
* Recursive solution is trivial, could you do it iteratively?
11+
12+
### Solution:
13+
* Method 1: dfs
14+
```Java
15+
/*
16+
// Definition for a Node.
17+
class Node {
18+
public int val;
19+
public List<Node> children;
20+
21+
public Node() {}
22+
23+
public Node(int _val,List<Node> _children) {
24+
val = _val;
25+
children = _children;
26+
}
27+
};
28+
*/
29+
class Solution {
30+
private List<Integer> result;
31+
public List<Integer> preorder(Node root) {
32+
this.result = new ArrayList<>();
33+
if(root == null) return result;
34+
dfs(root);
35+
return result;
36+
}
37+
private void dfs(Node node){
38+
if(node == null) return;
39+
result.add(node.val);
40+
for(int i = 0; i < node.children.size(); i++){
41+
dfs(node.children.get(i));
42+
}
43+
}
44+
}
45+
```
46+
47+
* Method 2: Iteration
48+
* We need to use stack to implement this question.
49+
* For example: [1,3,5,6,2,4]
50+
* step 1: we push 1 to stack, stack: [1]
51+
* step 2: push its child to stack in reverse order. stack: [3, 2, 4]
52+
* step 3: take the first element from stack, and save all its children into stack using step 2, stack: [5, 6, 2, 4]
53+
```Java
54+
/*
55+
// Definition for a Node.
56+
class Node {
57+
public int val;
58+
public List<Node> children;
59+
60+
public Node() {}
61+
62+
public Node(int _val,List<Node> _children) {
63+
val = _val;
64+
children = _children;
65+
}
66+
};
67+
*/
68+
class Solution {
69+
public List<Integer> preorder(Node root) {
70+
List<Integer> result = new ArrayList<>();
71+
if(root == null) return result;
72+
Stack<Node> stack = new Stack<>();
73+
stack.push(root);
74+
while(!stack.isEmpty()){
75+
Node node = stack.pop();
76+
result.add(node.val);
77+
for(int i = node.children.size() - 1; i >= 0; i--){
78+
stack.push(node.children.get(i));
79+
}
80+
}
81+
return result;
82+
}
83+
}
84+
```
85+
86+
### Reference
87+
1. [Leetcode 589. N-ary Tree Preorder Traversal](https://www.cnblogs.com/xiagnming/p/9591559.html)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## 590. N-ary Tree Postorder Traversal
2+
3+
### Question
4+
Given an n-ary tree, return the postorder traversal of its nodes' values.
5+
6+
For example, given a 3-ary tree:
7+
Return its postorder traversal as: [5,6,3,2,4,1].
8+
9+
10+
Note:
11+
* Recursive solution is trivial, could you do it iteratively?
12+
13+
### Solution:
14+
* Method 1: dfs
15+
```Java
16+
/*
17+
// Definition for a Node.
18+
class Node {
19+
public int val;
20+
public List<Node> children;
21+
22+
public Node() {}
23+
24+
public Node(int _val,List<Node> _children) {
25+
val = _val;
26+
children = _children;
27+
}
28+
};
29+
*/
30+
class Solution {
31+
private List<Integer> result;
32+
public List<Integer> postorder(Node root) {
33+
this.result = new ArrayList<>();
34+
if(root == null) return this.result;
35+
dfs(root);
36+
return this.result;
37+
}
38+
private void dfs(Node node){
39+
for(int i = 0; i < node.children.size(); i++){
40+
dfs(node.children.get(i));
41+
}
42+
result.add(node.val);
43+
}
44+
}
45+
```
46+
47+
* Method 2: Iteration
48+
```Java
49+
/*
50+
// Definition for a Node.
51+
class Node {
52+
public int val;
53+
public List<Node> children;
54+
55+
public Node() {}
56+
57+
public Node(int _val,List<Node> _children) {
58+
val = _val;
59+
children = _children;
60+
}
61+
};
62+
*/
63+
class Solution {
64+
public List<Integer> postorder(Node root) {
65+
List<Integer> result = new ArrayList<>();
66+
if(root == null) return result;
67+
Stack<Node> stack = new Stack<>();
68+
stack.push(root);
69+
while(!stack.isEmpty()){
70+
Node node = stack.pop();
71+
result.add(node.val);
72+
for(int i = 0; i < node.children.size(); i++){
73+
stack.push(node.children.get(i));
74+
}
75+
}
76+
Collections.reverse(result);
77+
return result;
78+
}
79+
}
80+
```

0 commit comments

Comments
 (0)