《剑指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 preorderRecursively(TreeNode root){//递归
        ArrayList result=new ArrayList<>();
        preoder(root,result);
        return result;
    }
    public static void preoder(TreeNode root,ArrayList 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 inorderRecursively(TreeNode root){
        ArrayList result=new ArrayList<>();
        inorder(root,result);
        return result;
    }
    public static void inorder(TreeNode root,ArrayList 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 postorderRecursively(TreeNode root){
        ArrayList result=new ArrayList<>();
        postorder(root,result);
        return result;
    }
    public static void postorder(TreeNode root,ArrayList 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 preorderIteratively(TreeNode root){//非递归
        ArrayList result=new ArrayList<>();
        Stack 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 inorderIteratively(TreeNode root){//非递归
        ArrayList result=new ArrayList<>();
        Stack 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 postorderIteratively(TreeNode root){//非递归
        ArrayList result=new ArrayList<>();
        Stack 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 levelorder(TreeNode root){
        ArrayList result=new ArrayList<>();
        ArrayDeque 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 list_preorderRecursively = preorderRecursively(root);
        System.out.print("preorderRecursively: "+'\t');
        System.out.println(list_preorderRecursively.toString());

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

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


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

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

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

        List 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

你可能感兴趣的:(剑指offer,笔记)