Skip to content

Commit 93cf897

Browse files
committed
Add solution 144
1 parent d74bb1c commit 93cf897

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Complete solutions to Leetcode problems, updated daily.
5151
| 075 | [Sort Colors](https://github.com/yanglbme/leetcode/tree/master/solution/075.Sort%20Colors) | `Array`, `Two Pointers`, `Sort` |
5252
| 082 | [Remove Duplicates from Sorted List II](https://github.com/yanglbme/leetcode/tree/master/solution/082.Remove%20Duplicates%20from%20Sorted%20List%20II) | `Linked List` |
5353
| 094 | [Binary Tree Inorder Traversal](https://github.com/yanglbme/leetcode/tree/master/solution/094.Binary%20Tree%20Inorder%20Traversal) | `Hash Table`, `Stack`, `Tree` |
54+
| 144 | [Binary Tree Preorder Traversal](https://github.com/yanglbme/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal) | `Stack`, `Tree` |
5455
| 153 | [Find Minimum in Rotated Sorted Array](https://github.com/yanglbme/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array) | `Array`, `Binary Search` |
5556

5657

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
## 二叉树的前序遍历
2+
### 题目描述
3+
4+
给定一个二叉树,返回它的 前序 遍历。
5+
6+
示例:
7+
```
8+
输入: [1,null,2,3]
9+
1
10+
\
11+
2
12+
/
13+
3
14+
15+
输出: [1,2,3]
16+
```
17+
18+
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
19+
20+
### 解法
21+
22+
- 递归算法
23+
24+
先访问根结点,再递归左子树,最后递归右子树。
25+
26+
```java
27+
/**
28+
* Definition for a binary tree node.
29+
* public class TreeNode {
30+
* int val;
31+
* TreeNode left;
32+
* TreeNode right;
33+
* TreeNode(int x) { val = x; }
34+
* }
35+
*/
36+
class Solution {
37+
public List<Integer> preorderTraversal(TreeNode root) {
38+
List<Integer> list = new ArrayList<>();
39+
preorderTraversal(root, list);
40+
return list;
41+
}
42+
43+
private void preorderTraversal(TreeNode root, List<Integer> list) {
44+
if (root == null) {
45+
return;
46+
}
47+
list.add(root.val);
48+
preorderTraversal(root.left, list);
49+
preorderTraversal(root.right, list);
50+
}
51+
}
52+
```
53+
54+
- 非递归算法
55+
循环:先访问根结点,接着判断是否有右孩子,有则压入栈中;再判断是否有左孩子,有则压入栈中。判断栈是否为空,若是,跳出循环。否则弹出栈顶元素,继续循环。
56+
57+
```java
58+
/**
59+
* Definition for a binary tree node.
60+
* public class TreeNode {
61+
* int val;
62+
* TreeNode left;
63+
* TreeNode right;
64+
* TreeNode(int x) { val = x; }
65+
* }
66+
*/
67+
class Solution {
68+
public List<Integer> preorderTraversal(TreeNode root) {
69+
List<Integer> list = new ArrayList<>();
70+
Stack<TreeNode> stack = new Stack<>();
71+
while (root != null) {
72+
list.add(root.val);
73+
if (root.right != null) {
74+
stack.push(root.right);
75+
}
76+
if (root.left != null) {
77+
stack.push(root.left);
78+
}
79+
if (!stack.isEmpty()) {
80+
root = stack.pop();
81+
} else {
82+
break;
83+
}
84+
}
85+
86+
return list;
87+
}
88+
}
89+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// 递归版本
2+
/*
3+
class Solution {
4+
public List<Integer> preorderTraversal(TreeNode root) {
5+
List<Integer> list = new ArrayList<>();
6+
preorderTraversal(root, list);
7+
return list;
8+
}
9+
10+
private void preorderTraversal(TreeNode root, List<Integer> list) {
11+
if (root == null) {
12+
return;
13+
}
14+
list.add(root.val);
15+
preorderTraversal(root.left, list);
16+
preorderTraversal(root.right, list);
17+
}
18+
}
19+
20+
*/
21+
22+
// 非递归版本
23+
class Solution {
24+
public List<Integer> preorderTraversal(TreeNode root) {
25+
List<Integer> list = new ArrayList<>();
26+
Stack<TreeNode> stack = new Stack<>();
27+
while (root != null) {
28+
list.add(root.val);
29+
if (root.right != null) {
30+
stack.push(root.right);
31+
}
32+
if (root.left != null) {
33+
stack.push(root.left);
34+
}
35+
if (!stack.isEmpty()) {
36+
root = stack.pop();
37+
} else {
38+
break;
39+
}
40+
}
41+
42+
return list;
43+
}
44+
}

0 commit comments

Comments
 (0)