给定一个二叉树,返回它的中序 遍历。
相关链接:
示例 1:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
二叉树的遍历问题都有2种解法,一种是递归,一种是迭代
递归:开启左子树递归,添加根节点,开启右子树递归
迭代:中序遍历一般等同于DFS,用Stack来实现,后进先出。迭代是先把左子树全部添加进Stack中,然后弹出一个尾部,获得对应的val之后遍历右子树,在添加左子树的时候同时也添加了根节点,所以pop弹出时再添加val实际上会对左子树和左子树的根节点进行操作
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
DFS(root);
return res;
}
public void DFS(TreeNode root){
if(root==null) return;
DFS(root.left);
res.add(root.val);
DFS(root.right);
}
}
/**
* 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<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode node = root;
while(node!=null||!stack.isEmpty()){
while(node!=null){
stack.push(node);
node = node.left;
}
node = stack.pop();
res.add(node.val);
node = node.right;
}
return res;
}
}