@@ -0,0 +1,1336 @@
+/* Check if a tree is balanced%0A * */%0A%0Apublic class P0412 %7B%0A  private class Node %7B%0A    int value;%0A    Node left, right;%0A%0A    public Node(int value) %7B%0A      this.value = value;%0A    %7D%0A  %7D%0A%0A  private int getHeight(Node node) %7B%0A    if (node == null)%0A      return 0;%0A%0A    return 1 + Math.max(getHeight(node.left), getHeight(node.right));%0A  %7D%0A%0A  public boolean isBalanced(Node root) %7B%0A    if (root == null)%0A      return true;%0A%0A    int leftHeight = 1 + getHeight(root.left);%0A    int rightHeight = 1 + getHeight(root.right); %0A%0A    //return Math.abs(leftHeight - rightHeight) %3C= 1 ? true : false;%0A    return isBalanced(root.right) && isBalanced(root.left);%0A  %7D%0A  /*%0A  public boolean isBalancedX(Node root) %7B%0A    if (root == null)%0A      return true;%0A  %7D%0A  */%0A%0A  public Node buildTree() %7B%0A    Node root = new Node(1);%0A    root.left = new Node(2);%0A    root.right = new Node(3);%0A    root.left.left = new Node(4);%0A    root.left.right = new Node(5);%0A    /*%0A    root.left.right.right = new Node(9);%0A    root.right.left = new Node(6);%0A    root.right.right= new Node(10);%0A    root.left.left.left = new Node(7);%0A    root.left.left.left.left = new Node(8);%0A    */%0A    //%0A    return root;%0A  %7D%0A%0A  public static void main(String%5B%5D args) %7B%0A    P0412 p0412 = new P0412();%0A    Node root = p0412.buildTree();%0A%0A    System.out.println(p0412.isBalanced(root));%0A  %7D%0A%7D%0A

check if a binary tree is balanced