Java实现二叉树的先序、中序、后序、层次遍历,数的最大深度、最大宽度

遍历、求最大深度、最大宽度的代码

package binaryTreeTest;

import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Traversal {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        TreeNode node0 = new TreeNode(0);

        node0.left = node1;
        node0.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node5;
        node2.right = node6;
        node6.left = node7;

        //preorder1(node0);
        System.out.println(width(node0));

    }

    //先序遍历递归版
    public static void preorder(TreeNode head){
        if(head == null)
            return;
        System.out.println(head.val);
        preorder(head.left);
        preorder(head.right);
    }
    //先序遍历非递归版
    public static void preorder1(TreeNode head){
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(head);
        System.out.println(head.val);

        while(!stack.isEmpty()){
            while(head.left != null){
                System.out.println(head.left.val);
                stack.push(head.left);
                head = head.left;
            }
            head = stack.pop();
            while(head.right == null){
                if(stack.isEmpty())
                    return;
                head = stack.pop();
            }
            System.out.println(head.right.val);
            stack.push(head.right);
            head = head.right;
        }

    }

    //中序遍历
    public static void inorder(TreeNode head){
        if(head == null)
            return;
        inorder(head.left);
        System.out.println(head.val);
        inorder(head.right);
    }

    //后序遍历
    public static void postorder(TreeNode head){
        if(head == null)
            return;
        postorder(head.left);
        postorder(head.right);
        System.out.println(head.val);
    }

    //层次遍历
    public static void levelorder(TreeNode head){
        Queue<TreeNode> queue = new LinkedBlockingQueue<TreeNode>();
        queue.offer(head);

        while(!queue.isEmpty()){
            System.out.println(queue.peek().val);
            if(queue.peek().left != null){
                queue.offer(queue.peek().left);
            }
            if(queue.peek().right != null){
                queue.offer(queue.peek().right);
            }
            queue.remove();
        }
    }

    //求树的最大深度
    public static int depth(TreeNode head){
        Queue<TreeNode> queue = new LinkedBlockingQueue<TreeNode>();
        int cur = 0;
        int last = 0;
        int level = 0;
        queue.offer(head);
        TreeNode tmp = new TreeNode(-1);
        while(!queue.isEmpty()){
            last = queue.size();
            cur = 0;
            while(cur < last){
                tmp = queue.poll();
                cur++;
                if(tmp.left != null){
                    queue.offer(tmp.left);
                }
                if(tmp.right != null){
                    queue.offer(tmp.right);
                }
            }

            level++;
        }

        return level;
    }

    //求树的最大宽度
    public static int width(TreeNode head){
        Queue<TreeNode> queue = new LinkedBlockingQueue<TreeNode>();
        int cur = 0;
        int last = 0;
        int maxWidth = 0;
        queue.offer(head);
        TreeNode tmp = new TreeNode(-1);
        while(!queue.isEmpty()){
            last = queue.size();
            maxWidth = Math.max(maxWidth, last);
            cur = 0;
            while(cur < last){
                tmp = queue.poll();
                cur++;
                if(tmp.left != null){
                    queue.offer(tmp.left);
                }
                if(tmp.right != null){
                    queue.offer(tmp.right);
                }
            }

        }

        return maxWidth;
    }

}

二叉树类

package binaryTreeTest;

public class TreeNode {

    int val;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val){
        this.val = val;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值