Skip to content

Commit a8de427

Browse files
committed
#94 二叉树中序遍历,递归版本和栈迭代版本
1 parent 697043c commit a8de427

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
/**
8+
* @author alis
9+
* @date 2019/10/26 12:32 AM
10+
* @description
11+
*/
12+
public class LC_94_InOrderTraversal {
13+
14+
/**
15+
* 利用栈迭代式中序遍历二叉树:左 根 右
16+
* 利用栈先进后出的特性
17+
* 先把根目录下的左结点,入栈,直至到最左结点(第一个输出的结点)
18+
* 判断出栈输出条件:栈顶
19+
* 如果已出栈结点存在右子结点,则右子结点入栈,并结束本次内循环,
20+
* 将本右子结点的所有左子结点入栈,重复以上步骤进行迭代循环
21+
*/
22+
public List<Integer> inOrderTraversal(TreeNode root) {
23+
List<Integer> nums = new ArrayList<>();
24+
if (null == root) return nums;
25+
Stack<TreeNode> stack = new Stack<>();
26+
stack.add(root);
27+
while (!stack.isEmpty()) {
28+
while (stack.peek().left != null) {
29+
stack.push(stack.peek().left);
30+
}
31+
while (!stack.isEmpty()) {
32+
TreeNode node = stack.pop();
33+
nums.add(node.val);
34+
if (node.right != null) {
35+
stack.push(node.right);
36+
break;
37+
}
38+
}
39+
}
40+
return nums;
41+
}
42+
43+
/**
44+
* 递归遍历二叉树,中序遍历:左 根 🈶右
45+
*/
46+
public List<Integer> inOrderTraversalReverse(TreeNode root) {
47+
List<Integer> nums = new ArrayList<>();
48+
if (nums == root) return nums;
49+
if (root.left != null) {
50+
nums.addAll(inOrderTraversalReverse(root.left));
51+
}
52+
nums.add(root.val);
53+
if (root.right != null) {
54+
nums.addAll(inOrderTraversalReverse(root.right));
55+
}
56+
return nums;
57+
}
58+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package tree;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.util.List;
7+
8+
/**
9+
* @author alis
10+
* @date 2019/10/26 12:24 AM
11+
* @description
12+
*/
13+
public class LC_94_InOrderTraversalTest {
14+
private LC_94_InOrderTraversal inOrderTraversal = new LC_94_InOrderTraversal();
15+
16+
@Test
17+
public void inOrderTraversalStackTest() {
18+
TreeNode root = new TreeNode(1);
19+
TreeNode root2 = new TreeNode(2);
20+
root2.left = new TreeNode(3);
21+
root.right = root2;
22+
List<Integer> nums = inOrderTraversal.inOrderTraversal(root);
23+
int[] result = new int[nums.size()];
24+
for (int i = 0; i < nums.size(); i++) {
25+
result[i] = nums.get(i);
26+
}
27+
Assert.assertArrayEquals(result, new int[]{1, 3, 2});
28+
}
29+
30+
@Test
31+
public void inOrderTraversalReverseTest() {
32+
TreeNode root = new TreeNode(1);
33+
TreeNode root2 = new TreeNode(2);
34+
root2.left = new TreeNode(3);
35+
root.right = root2;
36+
List<Integer> nums = inOrderTraversal.inOrderTraversalReverse(root);
37+
int[] result = new int[nums.size()];
38+
for (int i = 0; i < nums.size(); i++) {
39+
result[i] = nums.get(i);
40+
}
41+
Assert.assertArrayEquals(result, new int[]{1, 3, 2});
42+
}
43+
44+
}
45+
46+
/**
47+
* Definition for a binary tree node.
48+
*/
49+
class TreeNode {
50+
int val;
51+
TreeNode left;
52+
TreeNode right;
53+
54+
TreeNode(int x) {
55+
val = x;
56+
}
57+
}

0 commit comments

Comments
 (0)