(TreeNode root)
| 15 | */ |
| 16 | class Solution { |
| 17 | public boolean isBalanced(TreeNode root) { |
| 18 | |
| 19 | if(root==null){ |
| 20 | return true; |
| 21 | } |
| 22 | else{ |
| 23 | int left = height(root.left); |
| 24 | int right = height(root.right); |
| 25 | int diff = left - right; |
| 26 | diff = diff > 0 ? diff : -diff; // absolute |
| 27 | if(diff > 1){ |
| 28 | return false; |
| 29 | } |
| 30 | return isBalanced(root.left) && isBalanced(root.right); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | private int height(TreeNode root){ |
| 35 | // calculate the height of a tree |