GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/src/BinaryTreePreorderTraversal.java at master · LeBW/leetcode

Expand file tree

/

BinaryTreePreorderTraversal.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
74 lines (65 loc) · 2.54 KB
/

BinaryTreePreorderTraversal.java

Copy path

File metadata and controls

74 lines (65 loc) · 2.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
67
68
69
70
71
72
73
74
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* 144. Binary Tree Preorder Traversal
* @author LBW
*/
public class BinaryTreePreorderTraversal {
/**
* Method 1: Recursion. 递归解法
* @param root
* @return
*/
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
dfs(root, result);
return result;
}
private void dfs(TreeNode root, ArrayList<Integer> result) {
if (root == null)
return;
result.add(root.val);
dfs(root.left, result);
dfs(root.right, result);
}
/**
* Method 2: Iteration. 迭代解法
* @param root
* @return
*/
public List<Integer> preorderTraversalTwo(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
stack.push(root); // 先将根节点入栈
while (!stack.isEmpty()) { // 循环条件为:栈不为空
TreeNode cur = stack.pop(); // 弹出当前节点
if (cur != null) { //当前节点不为空时,先添加值进入结果数组,然后分别压入右节点和左节点(先右后左)
result.add(cur.val);
stack.push(cur.right);
stack.push(cur.left);
}
}
return result;
}
/**
* Method 3: Iteration 2:迭代解法2. 为了使前序遍历与中序遍历的形式相同,编写了一种类似的写法
*/
public List<Integer> preorderTraversalThree(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) { // 循环条件:cur不为空 或者 栈不为空。 其中cur不为空的情况出现在:左节点和根节点都访问完毕时,当前指向根节点的右节点时,栈恰好为空,但是此时还未结束。
if (cur != null) { //当cur不为空时,先输出其值,再将其压栈(为了后面能够进入其右节点),再访问它的左节点。
result.add(cur.val);
stack.push(cur);
cur = cur.left;
}
else { //当cur为空时,弹栈。此时栈顶元素已经被访问且输出过,所以此次直接进入其右节点即可。
cur = stack.pop();
cur = cur.right;
}
}
return result;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.