标签:左右 root == roo height mat 高度 val nbsp
特性:
它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树,同时,平衡二叉树必定是二叉搜索树,反之则不一定。
class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class Solution { public boolean isBalanced(TreeNode root) { if (root == null) return true; if (getHeight(root) == -1) return false; return true; } public int getHeight(TreeNode root) { if (root == null) return 0; int left = getHeight(root.left); int right = getHeight(root.right); if (left == -1 || right == -1) return -1; if (Math.abs(left - right) > 1) { return -1; } return Math.max(left, right) + 1; } }
标签:左右 root == roo height mat 高度 val nbsp
原文地址:http://www.cnblogs.com/hehe625/p/7773237.html