满二叉树

满二叉树
信息: 高度是H,节点数是N
条件: n=2^h-1

    public static boolean isFull(TreeNode head){
        Info info=process(head);
        int n=info.nodes;
        int h=info.height;
        // n=2^h-1
        return n== (1<<h)-1;
    }

    public static Info process(TreeNode x){
        if(x==null){
            //如返回null,process要处理空
            return new Info(0,0);
        }
        Info leftData=process(x.left);
        Info rightData=process(x.right);
        int height=Math.max(leftData.height,rightData.height)+1;
        int nodes= leftData.nodes+ rightData.nodes+1;
        return new Info(height,nodes);

    }

    public static class Info{
        public int height;
        public int nodes;
        public Info(int h,int n){
            this.height=h;
            this.nodes=n;
        }
    }

  

posted @ 2021-09-02 17:01  sherry001  阅读(89)  评论(0编辑  收藏  举报