二叉树的前、中、后序遍历

二叉树的前、中、后序遍历是二叉树结构的一个基础问题。遍历实现方法大体分为两种,递归与迭代。

首先说明前中后三种遍历的区别:

前序遍历:先访问根结点,再前序遍历根结点左子树,再前序遍历右子树。

中序遍历:先中序遍历访问根结点的左子树,再访问根结点,最后访问根结点的右子树。

后序遍历:先后序遍历访问根结点的左子树,再后序遍历访问根结点的右子树,最后访问根结点。

 

首先声明树结点TreeNode类。

  public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }
迭代:
前序遍历:
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(Integer.valueOf(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>res = new ArrayList<Integer>();
        if (root==null)
            return res;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode node = root;
        while (node!=null ||!stack.isEmpty()){
            if (node != null ){
                stack.push(node);
                node = node.left;
            }
            else{
                node = stack.pop();
                res.add(node.val);
                node = node.right;
            }
        }
        return res;
    }
}

后序遍历:

class Solution {
    public List<Integer> postorderTraversal(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();
            if (node.left!=null)
                stack.push(node.left);
            if (node.right!=null)
                stack.push(node.right);
            res.add(0,node.val);//逆序添加结点值
        }
        return res;
    }
}

其中后序遍历是一种取巧的方法,利用先序遍历“根左右”的遍历顺序,将先序遍历顺序更改为“根右左”,逆序添加结点值来反转结果res,得到结果顺序为“左右根”。

 

递归:

前序遍历:

class Solution {
    public List<Integer> res = new ArrayList<Integer>();
    public List<Integer> preorderTraversal(TreeNode root) {
        if (root==null)
            return res;
        res.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return res;
    }
}

中序遍历:

class Solution {
    public List<Integer> res = new ArrayList<Integer>();
    public List<Integer> inorderTraversal(TreeNode root) {
        if (root==null)
            return res;
        inorderTraversal(root.left);
        res.add(root.val);
        inorderTraversal(root.right);
        return res;
    }
}

后序遍历:

class Solution{
    public List<Integer> res = new ArrayList<Integer>();
    public List<Integer> postorderTraversal(TreeNode root) {
        if(root == null)
            return res;
        postorderTraversal(root.left);
        postorderTraversal(root.right);
        res.add(root.val);
        return res;
    }
}

 

posted @ 2020-05-20 19:06  zjcfrancis  阅读(608)  评论(0编辑  收藏  举报