《剑指offer》面试题:二叉树的遍历总结

二叉树的遍历总结


二叉树是一种非常常用的数据结构,也是面试的热门词。而二叉树最常见的考点莫过于遍历,剑指offer的第60页介绍树时也着重强调了二叉树遍历的重要性,但书中并未实现。本文将完整地实现二叉树遍历。

方法名称主要功能更呢
preorderRecursively前序遍历递归版
inorderRecursively中序遍历递归版
postorderRecursively后序遍历递归版
preorderIteratively前序遍历非递归版
inorderIteratively中序遍历非递归版
postorderIteratively后序遍历非递归版
levelorder层序遍历/宽度优先遍历

Java参考代码如下:

package chapter2;
import java.util.*;

public class P60_TraversalOfBinaryTree {
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int val){
            this.val=val;
            this.left=null;
            this.right=null;
        }
    }
    //前序遍历递归版
    public static ArrayList<Integer> preorderRecursively(TreeNode root){//递归
        ArrayList<Integer> result=new ArrayList<>();
        preoder(root,result);
        return result;
    }
    public static void preoder(TreeNode root,ArrayList<Integer> result){
        if(root==null) return;
        result.add(root.val);
        if(root.left!=null) preoder(root.left,result);
        if(root.right!=null) preoder(root.right,result);
    }

    //中序遍历递归版
    public static ArrayList<Integer> inorderRecursively(TreeNode root){
        ArrayList<Integer> result=new ArrayList<>();
        inorder(root,result);
        return result;
    }
    public static void inorder(TreeNode root,ArrayList<Integer> result){//递归
        if(root==null) return;
        if(root.left!=null) inorder(root.left,result);
        result.add(root.val);
        if(root.right!=null) inorder(root.right,result);
    }

    //后序遍历递归版
    public static ArrayList<Integer> postorderRecursively(TreeNode root){
        ArrayList<Integer> result=new ArrayList<>();
        postorder(root,result);
        return result;
    }
    public static void postorder(TreeNode root,ArrayList<Integer> result){//递归
        if(root==null) return;
        if(root.left!=null) postorder(root.left,result);
        if(root.right!=null) postorder(root.right,result);
        result.add(root.val);
    }

    //前序遍历非递归版
    public static ArrayList<Integer> preorderIteratively(TreeNode root){//非递归
        ArrayList<Integer> result=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();//ArrayDeque.addFirst\.pollFirst
        TreeNode p=root;
        while (!stack.isEmpty()||p!=null){
            if(p!=null){
                stack.add(p);
                result.add(p.val);
                p=p.left;
            }else {
                TreeNode t=stack.pop();
                p=t.right;
            }
        }
        return result;
    }

    //中序遍历非递归版
    public static ArrayList<Integer> inorderIteratively(TreeNode root){//非递归
        ArrayList<Integer> result=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();//ArrayDeque.addFirst\.pollFirst
        TreeNode p=root;
        while(!stack.isEmpty()||p!=null){
            if(p!=null){
                stack.add(p);
                p=p.left;
            }else {
                TreeNode t=stack.pop();
                result.add(t.val);
                p=t.right;
            }
        }
        return result;
    }

    //后序遍历非递归版
    public static ArrayList<Integer> postorderIteratively(TreeNode root){//非递归
        ArrayList<Integer> result=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();//ArrayDeque.addFirst\.pollFirst
        TreeNode p=root;
        while(!stack.isEmpty()||p!=null){
            if(p!=null){
                stack.add(p);
                result.add(0,p.val);//从头插入,可用ArrayDeque,addFirst快。
                p=p.right;
            }else {
                TreeNode t=stack.pop();
                p=t.left;
            }
        }
        return result;
    }

    //层序遍历
    public static ArrayList<Integer> levelorder(TreeNode root){
        ArrayList<Integer> result=new ArrayList<>();
        ArrayDeque<TreeNode> queue=new ArrayDeque<>();
        queue.addLast(root);
        while (!queue.isEmpty()){
        	//for(int i=queue.size();i>=0;i--)//level
            TreeNode p=queue.pollFirst();
            result.add(p.val);
            if(p.left!=null)
                queue.addLast(p.left);
            if(p.right!=null)
                queue.addLast(p.right);
        }
        return result;
    }

    public static void main(String[] args){
        TreeNode root=new TreeNode(1);
        root.left=new TreeNode(2);
        root.right=new TreeNode(3);
        root.left.left=new TreeNode(4);
        root.left.right=new TreeNode(5);
        root.right.left=new TreeNode(6);
        root.right.right=new TreeNode(7);

        List<Integer> list_preorderRecursively = preorderRecursively(root);
        System.out.print("preorderRecursively: "+'\t');
        System.out.println(list_preorderRecursively.toString());

        List<Integer> list_inorderRecursively = inorderRecursively(root);
        System.out.print("inorderRecursively: "+'\t');
        System.out.println(list_inorderRecursively.toString());

        List<Integer> list_postorderRecursively = postorderRecursively(root);
        System.out.print("postorderRecursively: "+'\t');
        System.out.println(list_postorderRecursively.toString());
        System.out.println();


        List<Integer> list_preorderIteratively = preorderIteratively(root);
        System.out.print("preorderIteratively: "+'\t');
        System.out.println(list_preorderIteratively.toString());

        List<Integer> list_inorderIteratively = inorderIteratively(root);
        System.out.print("inorderIteratively: "+'\t');
        System.out.println(list_inorderIteratively.toString());

        List<Integer> list_postorderIteratively = postorderIteratively(root);
        System.out.print("postorderIteratively: "+'\t');
        System.out.println(list_postorderIteratively.toString());
        System.out.println();

        List<Integer> list_levelorder = levelorder(root);
        System.out.print("levelorder: "+'\t');
        System.out.println(list_levelorder.toString());
    }
}


参考:
https://www.jianshu.com/p/362d4ff42ab2
http://www.cnblogs.com/grandyang/p/4146981.html
http://www.cnblogs.com/grandyang/p/4297300.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值