package com.thinker.test.test4;

public class Test_4_1 {

    static class Tree {
        Tree left;
        Tree right;
        int obj;
        public Tree(int obj){
            this.obj = obj;
        }

    }

    public static int checkHeight(Tree tree) {
        if (tree == null) return 0;
        int left = checkHeight(tree.left);
        if (left == -1) return -1;
        int right = checkHeight(tree.right);
        if (right == -1) return -1;
        if (Math.abs(left - right) > 1) return -1;
        else return Math.max(left, right) + 1;
    }

    public static void main(String[] args) {
        Tree root = new Tree(1);
        root.left = new Tree(3);
        root.right = new Tree(4);
        root.left.left = new Tree(6);
        root.left.left.left = new Tree(7);
        int result = checkHeight(root);
        System.out.println(result);
    }
}
