题目描述

给定一个二叉树,返回它的中序 遍历。

示例:

1
2
3
4
5
6
7
8
输入: [1,null,2,3]
1
\
2
/
3

输出: [1,3,2]

进阶:递归算法很简单,你可以通过迭代算法完成吗?

思路 + 代码

递归方法

递归截止条件:root==null

递归执行条件:
list.add(inorderTraversal(root.left))
list.add(root.val)
list.add(inorderTraversal(root.right))

代码

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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List <Integer> inorderTraversal(TreeNode root) {
List <Integer> res = new ArrayList <> ();
helper(root, res);
return res;
}

public void helper(TreeNode root, List <Integer> res) {
if (root != null) {
if (root.left != null) {
helper(root.left, res);
}
res.add(root.val);
if (root.right != null) {
helper(root.right, res);
}
}
}
}

迭代方法

用一个栈存储遍历过得父子节点,循环遍历。

代码

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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
// 当树没有遍历完全
while(root!=null || !stack.isEmpty()){
// 找到当前树的最左侧叶子节点(父节点)
while(root!=null){
stack.push(root);
root=root.left;
}
// 中序遍历
root = stack.pop();
list.add(root.val);
root = root.right;
}
return list;
}
}

题目链接