39 二叉树的深度和宽度以及判断是否是平衡二叉树

描述:


实现代码:

package question39;

import java.util.Scanner;





class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
public class TreeDepth {
    static int c1=0,c2=0;
    // 创建二叉树
    private static TreeNode creatTree(Scanner in) {
        // TODO Auto-generated method stub
        TreeNode newNode;
        int val=in.nextInt();
        if (val==-1) {     //设置 -1 为结束数字
            val=in.nextInt();
        }
        if (val!=0) {    // 0 代表空节点
            newNode=new TreeNode(val);
        }else {
            newNode=null;
            return null;
        }

        newNode.left=creatTree(in);
        newNode.right=creatTree(in);

        return newNode;

    }
    // 打印二叉树
    private static void display(TreeNode root) {
        // TODO Auto-generated method stub

        if (root!=null) {
            System.out.print(root.val+"  ");
            display( root.left);
            display(root.right);

        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
       
        System.out.println("请输入树 的先序遍历:");
        TreeNode root=creatTree(in);
        System.out.println("下面是树 的先序遍历:");
        display(root);
        System.out.println();
       int high=treeDepth(root);
       int width=treeWidth(root);
       Math.abs(2);
       System.out.println("depth of the tree is  :"+high);
       System.out.println("width of the tree is  :"+width);
       boolean isBalanced=isBalancedTree(root);
       System.out.println("方法一:isBalanced of the tree is  :"+isBalanced+"  遍历的总次数   "+c1);
       
       
       boolean isBalanced2=IsBalanced_Solution2(root);
       System.out.println("方法二:isBalanced2 of the tree is  :"+isBalanced2+"   遍历的总次数   "+c2);
    }
    private static boolean IsBalanced_Solution2(TreeNode root) {
        // TODO Auto-generated method stub
        int[] pDepth = new int[1];
         return IsBalanced(root,pDepth);
    }
    private static boolean IsBalanced(TreeNode pRoot, int[] pDepth) {
        c2++;
        // TODO Auto-generated method stub
         if(pRoot == null)
            {
                pDepth[0] = 0;
                return true;
            }

            int left[] = new int[1];
            int[] right = new int[1];
            if(IsBalanced(pRoot.left,left) 
                && IsBalanced(pRoot.right,right))
            {
                int diff = left[0] - right[0];
                if(diff <= 1 && diff >= -1)
                {
                    pDepth[0] = 1 + (left[0] > right[0] ? left[0] : right[0]);
                    return true;
                }
            }

            return false;
    }
    private static boolean isBalancedTree(TreeNode root) {
        c1++;
        // TODO Auto-generated method stub
        if (root==null) {
            return true;
        }
        int left=treeDepth(root.left);
        int right=treeDepth(root.right);
        if (Math.abs(left-right)<2) {
            return isBalancedTree(root.left)&&isBalancedTree(root.right);
        }
        
        return false;
    }
    private static int treeWidth(TreeNode pRoot) {
        // TODO Auto-generated method stub
        if(pRoot==null) return 0;
        if (pRoot.left==null&&pRoot.right==null) {
            return 1;
        }
        int left=treeWidth(pRoot.left);
        int right=treeWidth(pRoot.right);
        return left+right;
    }
    private static int treeDepth(TreeNode pRoot) {
        c1++;
        // TODO Auto-generated method stub
          if(pRoot==null) return 0;
             int left=treeDepth(pRoot.left);
             int right=treeDepth(pRoot.right);
             return (left>right)? (left+1):(right+1);
    }

}




测试结果:


最后多啰嗦几句,方法二在因为求深度的函数会遍历树的节点,所以遍历次数大大增加。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值