判断是否是满二叉树

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }
    
    public static boolean isFull(Node head){
        ReturnData allInfo=process(head, 1);
        return ((1<<allInfo.level)-1==allINfo.nums;
    }
    public static class ReturnData{
        public int level;
        public int nums;
        public ReturnData(int l, int n){
            level=l;
            nums=n;
        }
    }
    public static ReturnData process(Node head, int level){
        if(head==null){
            return new ReturnData(level, 0);
        }
        ReturnData leftInfo = process(head.left, level+1);
        ReturnData rightInfo = process(head.right, level+1);
        int nums = leftInfo.nums + rightInfo.nums+1;
        int deep = Math.max(leftInfo.level,rightInfo.level);
        return new ReturnData(deep, nums);
    }
        public static class ReturnType {
        public boolean isBalanced;
        public int height;

        public ReturnType(boolean isB, int hei) {
            isBalanced = isB;
            height = hei;
        }
    }

 

posted @ 2020-01-30 15:03  砍柴人Ryan  阅读(609)  评论(0编辑  收藏  举报