ÌâÄ¿ Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. ˼·Ê×ÏÈ£¬Àí½âÌâÄ¿£¬ÅжÏÊÇ·ñΪ¶þ²æÊ÷¸ß¶ÈÊÇ·ñƽºâµÄ±ê×¼ÊÇÿ¸ö½ÚµãµÄ×óÓÒ×ÓÊ÷Ïà²î²»µÃ³¬¹ý1 Æä´Î£¬Àí½âÌâÄ¿ºó£¬ºÜÈÝÒ×Ïëµ½ÕâÊÇÒ»µÀµäÐ͵ĵݹéÌâÄ¿£¨ps:ÌØ±ðÊÇÃæÊÔµÄʱºò£¬¶þ²æÊ÷ºÍµÝ¹éÁªÏµµÄÌØ±ð¶à£¬ÎÒÃæÊÔÍã¶¹¼ÔÒ²ÊÇ¿¼ÁËÒ»¸öÀàËÆµÄÌâÄ¿£© ×îºó£¬Ê×ÏÈÅжϸù½ÚµãµÄ×óÓÒ×ÓÊ÷¸ß¶È²î£¬Ïà²î´óÓÚ1·µ»Øfalse,Ïà²î²»´óÓÚ1Ôò¼ÌÐøµÝ¹éµÄ²âÊÔ¸ù½ÚµãµÄ×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷ AC´úÂë public class BalancedBinaryTree { static class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode£¨int x£© { this.val = x; } } public static boolean isBalanced£¨TreeNode root£© { if £¨root == null£© return true; int lh = getHeight£¨root.left£©£» int rh = getHeight£¨root.right£©£» if £¨Math.abs£¨lh - rh£© > 1£© return false; return isBalanced£¨root.left£© && isBalanced£¨root.right£©£» } public static int getHeight£¨TreeNode root£© { if £¨root == null£© return 0; return Math.max£¨getHeight£¨root.left£©£¬ getHeight£¨root.right£©£© + 1; } } ÍÂ²Û csdnÉñÂíʱºò²ÅÄÜÖ§³ÖmarkdownÓï·¨±à¼²©¿Í°¡£¬Èç¹ûµ½¹ýÄ껹²»Ö§³Ömarkdown,¹û¶ÏÇ¨ÒÆµ½segmentfaultÉÏÁË |