-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0094_BinaryTreeInorderTraversal.java
More file actions
79 lines (72 loc) · 2.46 KB
/
P0094_BinaryTreeInorderTraversal.java
File metadata and controls
79 lines (72 loc) · 2.46 KB
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
75
76
77
78
79
package yyl.leetcode.p00;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import yyl.leetcode.bean.TreeNode;
/**
* <h3>二叉树的中序遍历</h3><br>
* 给定一个二叉树,返回它的中序 遍历。<br>
*
* <pre>
* 示例:
* 输入: [1,null,2,3]
* 1
* \
* 2
* /
* 3
* 输出: [1,3,2]
* </pre>
*
* 进阶: 递归算法很简单,你可以通过迭代算法完成吗?<br>
*/
public class P0094_BinaryTreeInorderTraversal {
public static void main(String[] args) {
Solution solution = new Solution();
TreeNode root = TreeNode.create("[1,null,2,3]");
System.out.println(solution.inorderTraversal(root));
}
// 迭代
// 时间复杂度:O(N),虽然时间复杂度和递归一致,但是其实每个节点需要入栈出栈,遍历了两次
// 时间复杂度:O(N),存储返回结果
static class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
ArrayDeque<TreeNode> stack = new ArrayDeque<>();
do {
// root(或者当前右子节点)不为空,迭代遍历左侧子树,入栈
while (root != null) {
stack.push(root);
root = root.left;
}
// 栈不为空
if (!stack.isEmpty()) {
// 弹出,这个node是未处理的最左节点
TreeNode node = stack.pop();
// 处理
result.add(node.val);
// 然后将root设置为右子节点
root = node.right;
}
} while (!stack.isEmpty() || root != null);
return result;
}
}
// 递归
// 时间复杂度:O(N),每个节点遍历一次
// 时间复杂度:O(N),存储返回结果
static class Solution1 {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
ldr(root, result);
return result;
}
private void ldr(TreeNode root, List<Integer> result) {
if (root != null) {
ldr(root.left, result);
result.add(root.val);
ldr(root.right, result);
}
}
}
}