-
Notifications
You must be signed in to change notification settings - Fork 0
/
144. 树的前序遍历.java
66 lines (60 loc) · 1.84 KB
/
144. 树的前序遍历.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package 树的遍历;
// 144. 二叉树的前序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
/**
* 前序,递归
*/
// class Solution {
// public List<Integer> preorderTraversal(TreeNode root) {
// List<Integer> preorderList = new ArrayList<>();
// preorderTree(preorderList, root);
// return preorderList;
// }
// private void preorderTree(List<Integer> list, TreeNode root){
// if(root == null){
// return;
// }else{
// list.add(root.val);
// preorderTree(list, root.left);
// preorderTree(list, root.right);
// }
// }
// }
/**
* 前序,迭代
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> preorderList = new ArrayList<>();
if(root == null){
return preorderList;
}
Stack<TreeNode> treeStack = new Stack<>();
treeStack.push(root);
while(!treeStack.isEmpty()){
TreeNode tempNode = treeStack.pop();
preorderList.add(tempNode.val);
if(tempNode.right != null){
treeStack.push(tempNode.right);
}
if(tempNode.left != null){
treeStack.push(tempNode.left);
}
}
return preorderList; // 迭代的push过程是先根,再right,再left,因此迭代的遍历过程为“根左右”。因为先push其right,再push其left,当pop时,就会先pop left,再pop right,形成了“根左右”
}
}