author: Blankj blog : http://blankj.com time : 2017/10/09 desc :
| 12 | * </pre> |
| 13 | */ |
| 14 | public class Solution { |
| 15 | public boolean isBalanced(TreeNode root) { |
| 16 | return helper(root) != -1; |
| 17 | } |
| 18 | |
| 19 | private int helper(TreeNode node) { |
| 20 | if (node == null) return 0; |
| 21 | int l = helper(node.left); |
| 22 | if (l == -1) return -1; |
| 23 | int r = helper(node.right); |
| 24 | if (r == -1) return -1; |
| 25 | if (Math.abs(l - r) > 1) return -1; |
| 26 | return 1 + Math.max(l, r); |
| 27 | } |
| 28 | |
| 29 | public static void main(String[] args) { |
| 30 | Solution solution = new Solution(); |
| 31 | TreeNode testData = TreeNode.createTestData("[1,2,2,3,3,3,3,4,4,4,4,4,4,null,null,5,5]"); |
| 32 | TreeNode.print(testData); |
| 33 | System.out.println(solution.isBalanced(testData)); |
| 34 | } |
| 35 | } |
nothing calls this directly
no outgoing calls
no test coverage detected