Skip to content

Commit d9c8ead

Browse files
committed
feat: add solutions to leetcode problems: No.0094,No.0144,No.0145
1 parent e62eb43 commit d9c8ead

File tree

12 files changed

+424
-165
lines changed

12 files changed

+424
-165
lines changed

solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md

+51-2
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,71 @@
2424

2525
<!-- 这里可写通用的实现逻辑 -->
2626

27+
递归遍历。
28+
2729
<!-- tabs:start -->
2830

2931
### **Python3**
3032

3133
<!-- 这里可写当前语言的特殊实现逻辑 -->
3234

3335
```python
34-
36+
# Definition for a binary tree node.
37+
# class TreeNode:
38+
# def __init__(self, val=0, left=None, right=None):
39+
# self.val = val
40+
# self.left = left
41+
# self.right = right
42+
class Solution:
43+
def inorderTraversal(self, root: TreeNode) -> List[int]:
44+
def inorder(root):
45+
if root:
46+
inorder(root.left)
47+
res.append(root.val)
48+
inorder(root.right)
49+
res = []
50+
inorder(root)
51+
return res
3552
```
3653

3754
### **Java**
3855

3956
<!-- 这里可写当前语言的特殊实现逻辑 -->
4057

4158
```java
42-
59+
/**
60+
* Definition for a binary tree node.
61+
* public class TreeNode {
62+
* int val;
63+
* TreeNode left;
64+
* TreeNode right;
65+
* TreeNode() {}
66+
* TreeNode(int val) { this.val = val; }
67+
* TreeNode(int val, TreeNode left, TreeNode right) {
68+
* this.val = val;
69+
* this.left = left;
70+
* this.right = right;
71+
* }
72+
* }
73+
*/
74+
class Solution {
75+
76+
private List<Integer> res;
77+
78+
public List<Integer> inorderTraversal(TreeNode root) {
79+
res = new ArrayList<>();
80+
inorder(root);
81+
return res;
82+
}
83+
84+
private void inorder(TreeNode root) {
85+
if (root != null) {
86+
inorder(root.left);
87+
res.add(root.val);
88+
inorder(root.right);
89+
}
90+
}
91+
}
4392
```
4493

4594
### **...**

solution/0000-0099/0094.Binary Tree Inorder Traversal/README_EN.md

+49-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,60 @@
3535
### **Python3**
3636

3737
```python
38-
38+
# Definition for a binary tree node.
39+
# class TreeNode:
40+
# def __init__(self, val=0, left=None, right=None):
41+
# self.val = val
42+
# self.left = left
43+
# self.right = right
44+
class Solution:
45+
def inorderTraversal(self, root: TreeNode) -> List[int]:
46+
def inorder(root):
47+
if root:
48+
inorder(root.left)
49+
res.append(root.val)
50+
inorder(root.right)
51+
res = []
52+
inorder(root)
53+
return res
3954
```
4055

4156
### **Java**
4257

4358
```java
44-
59+
/**
60+
* Definition for a binary tree node.
61+
* public class TreeNode {
62+
* int val;
63+
* TreeNode left;
64+
* TreeNode right;
65+
* TreeNode() {}
66+
* TreeNode(int val) { this.val = val; }
67+
* TreeNode(int val, TreeNode left, TreeNode right) {
68+
* this.val = val;
69+
* this.left = left;
70+
* this.right = right;
71+
* }
72+
* }
73+
*/
74+
class Solution {
75+
76+
private List<Integer> res;
77+
78+
public List<Integer> inorderTraversal(TreeNode root) {
79+
res = new ArrayList<>();
80+
inorder(root);
81+
return res;
82+
}
83+
84+
private void inorder(TreeNode root) {
85+
if (root != null) {
86+
inorder(root.left);
87+
res.add(root.val);
88+
inorder(root.right);
89+
}
90+
}
91+
}
4592
```
4693

4794
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,33 @@
1-
// 递归版本
2-
/*
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
316
class Solution {
4-
public List<Integer> inorderTraversal(TreeNode root) {
5-
List<Integer> list = new ArrayList<>();
6-
inorderTraversal(root, list);
7-
return list;
8-
}
9-
10-
private void inorderTraversal(TreeNode root, List<Integer> list) {
11-
if (root == null) {
12-
return;
13-
}
14-
inorderTraversal(root.left, list);
15-
list.add(root.val);
16-
inorderTraversal(root.right, list);
17-
}
18-
19-
}
2017

21-
*/
18+
private List<Integer> res;
2219

23-
// 非递归版本
24-
class Solution {
2520
public List<Integer> inorderTraversal(TreeNode root) {
26-
List<Integer> list = new ArrayList<>();
27-
Stack<TreeNode> stack = new Stack<>();
28-
29-
while (root != null || !stack.isEmpty()) {
30-
while (root != null) {
31-
stack.push(root);
32-
root = root.left;
33-
}
34-
if (!stack.isEmpty()) {
35-
root = stack.pop();
36-
list.add(root.val);
37-
root = root.right;
38-
}
21+
res = new ArrayList<>();
22+
inorder(root);
23+
return res;
24+
}
25+
26+
private void inorder(TreeNode root) {
27+
if (root != null) {
28+
inorder(root.left);
29+
res.add(root.val);
30+
inorder(root.right);
3931
}
40-
return list;
41-
4232
}
43-
4433
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,16 @@
11
# Definition for a binary tree node.
22
# class TreeNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.left = None
6-
# self.right = None
7-
8-
# 递归版本
9-
"""
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
107
class Solution:
11-
def inorderTraversal(self, root):
12-
"""
13-
: type root: TreeNode
14-
: rtype: List[int]
15-
"""
16-
def dp(node):
17-
if not node:
18-
return
19-
dp(node.left)
20-
ls.append(node.val)
21-
dp(node.right)
22-
ls=[]
23-
dp(root)
24-
return ls
25-
"""
26-
27-
# 非递归版本
28-
class Solution:
29-
def inorderTraversal(self, root):
30-
"""
31-
:type root: TreeNode
32-
:rtype: List[int]
33-
"""
34-
ls = []
35-
stack = []
36-
while root or stack:
37-
while root:
38-
stack.append(root)
39-
root = root.left
40-
if stack:
41-
root = stack.pop(-1)
42-
ls.append(root.val)
43-
root = root.right
44-
return ls
8+
def inorderTraversal(self, root: TreeNode) -> List[int]:
9+
def inorder(root):
10+
if root:
11+
inorder(root.left)
12+
res.append(root.val)
13+
inorder(root.right)
14+
res = []
15+
inorder(root)
16+
return res

solution/0100-0199/0144.Binary Tree Preorder Traversal/README.md

+51-2
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,71 @@
2525

2626
<!-- 这里可写通用的实现逻辑 -->
2727

28+
递归遍历。
29+
2830
<!-- tabs:start -->
2931

3032
### **Python3**
3133

3234
<!-- 这里可写当前语言的特殊实现逻辑 -->
3335

3436
```python
35-
37+
# Definition for a binary tree node.
38+
# class TreeNode:
39+
# def __init__(self, val=0, left=None, right=None):
40+
# self.val = val
41+
# self.left = left
42+
# self.right = right
43+
class Solution:
44+
def preorderTraversal(self, root: TreeNode) -> List[int]:
45+
def preorder(root):
46+
if root:
47+
res.append(root.val)
48+
preorder(root.left)
49+
preorder(root.right)
50+
res = []
51+
preorder(root)
52+
return res
3653
```
3754

3855
### **Java**
3956

4057
<!-- 这里可写当前语言的特殊实现逻辑 -->
4158

4259
```java
43-
60+
/**
61+
* Definition for a binary tree node.
62+
* public class TreeNode {
63+
* int val;
64+
* TreeNode left;
65+
* TreeNode right;
66+
* TreeNode() {}
67+
* TreeNode(int val) { this.val = val; }
68+
* TreeNode(int val, TreeNode left, TreeNode right) {
69+
* this.val = val;
70+
* this.left = left;
71+
* this.right = right;
72+
* }
73+
* }
74+
*/
75+
class Solution {
76+
77+
private List<Integer> res;
78+
79+
public List<Integer> preorderTraversal(TreeNode root) {
80+
res = new ArrayList<>();
81+
preorder(root);
82+
return res;
83+
}
84+
85+
private void preorder(TreeNode root) {
86+
if (root != null) {
87+
res.add(root.val);
88+
preorder(root.left);
89+
preorder(root.right);
90+
}
91+
}
92+
}
4493
```
4594

4695
### **...**

0 commit comments

Comments
 (0)