题目地址

题目描述

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

示例:

输入: [1,null,2,3]
1

2
/
3

输出: [1,2,3]

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

解法

cpp
迭代

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
     List<Integer> res = new ArrayList<Integer>();
      if (root == null) {
            return res;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            res.add(node.val);
  
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
        return res;
    }
}

递归

class Solution {
      List<Integer> ans= new LinkedList<>();
    public List<Integer> preorderTraversal(TreeNode root) {
        helper(root,ans);
        return ans;
    }

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

解题思路

递归

二叉树的前序遍历,访问顺序 根->左->右,所以使用递归解法可以按照这个思路进行写

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

中序遍历 左->根->右

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

后序遍历 左->右->根

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

其实只要移动ans.add(root.val);的位置便可以实现不同的遍历,但是迭代的写法却不能这么简单,所以还是喜欢递归的写法

迭代

递归的解法,需要借助外部空间(栈或者队列);如果我们使用栈的解法,
1、 前序遍历,访问顺序 根->左->右
2、栈的特点,LIFO(后进先出),所以前序遍历,我们先将右子树压入,再压入左子树

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
     List<Integer> res = new ArrayList<Integer>();
      if (root == null) {
            return res;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            res.add(node.val);
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
        return res;
    }
}

中序遍历

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
  List<Integer> ans=new LinkedList<>();
        Stack<TreeNode> stack=new Stack();
        TreeNode curr = root;
        while (curr != null || !stack.isEmpty()) {
            while (curr != null) {
                stack.push(curr);
                curr = curr.left;
            }
            curr = stack.pop();
            ans.add(curr.val);
            curr = curr.right;
        }
        return ans;
    }
}

后序遍历

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
  List<Integer> res = new ArrayList<>();
            Stack<TreeNode> stack=new Stack<>();
            Stack<Integer> tmp = new Stack<>();
            while(root != null || !stack.empty()){
                while (root != null) {
                    tmp.add(root.val);
                    stack.push(root);
                    root = root.right;
                }
                if (!stack.empty()) {
                    root = stack.pop().left;
                }
            }
            while(!tmp.empty()){
                res.add(tmp.pop());
            }
            return res;
    }
}