欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

判断是否是满二叉树

程序员文章站 2024-03-18 22:12:22
...
public class fullBT {

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

        public Node(int value) {
            this.value = value;
        }
    }


    public static class ReturnType{
        public int nodes;
        public  int height;
        public ReturnType(int nodes,int height){
            this.height=height;
            this.nodes=nodes;
        }
    }

    public static boolean isFull(Node head){
        ReturnType result= process(head);
        System.out.println(result.nodes+"--"+result.height);
        return result.nodes==Math.pow(2,result.height)-1 ;
    }

    public static ReturnType process(Node x){
        if(x==null){
            return new ReturnType(0,0);
        }
        ReturnType leftData=process(x.left);
        ReturnType rightData=process(x.right);
        int height=Math.max(leftData.height,rightData.height)+1;
        int nodes=leftData.nodes+rightData.nodes+1;
        return new ReturnType(nodes,height);

    }

    public static void main(String[] args) {
        Node head=new Node(5);
        head.left=new Node(3);
        head.right=new Node(8);
        head.left.left=new Node(2);
        head.left.right=new Node(4);
        head.left.right=new Node(4);
        head.right.left=new Node(7);
        head.right.right=new Node(9);
        Node a=null;
        System.out.println(isFull(a));


    }
}