标签:
1 public class Solution { 2 public boolean isBalanced(TreeNode root) { 3 int result = checkHeight(root); 4 return result != -1; 5 } 6 7 private int checkHeight(TreeNode root) { 8 if(root == null) return 0; 9 int left = checkHeight(root.left); 10 if(left == -1) return -1; 11 int right = checkHeight(root.right); 12 if(right == -1) return -1; 13 if(Math.abs(left - right) > 1) { 14 return -1; 15 } else { 16 return Math.max(left, right) + 1; 17 } 18 } 19 }
标签:
原文地址:http://www.cnblogs.com/diyishao/p/4300591.html