ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

¶þ²æÊ÷¸ß¶ÈµÄƽºâ±ê×¼
2013-12-05 13:05:50 À´Ô´: ×÷Õß: ¡¾´ó ÖРС¡¿ ä¯ÀÀ:163´Î
Tags£º¸ß¶È ƽºâ ±ê×¼
    ÌâÄ¿ 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ÉÏÁË

¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
·ÖÏíµ½: 
ÉÏһƪ£ºC++Öо²Ì¬³õʼ»¯µÄÏàÒÀÐÔ ÏÂһƪ£º¶þ½øÖÆÎ»×îºó³öÏÖ1µÄ¸ÅÂÊ

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ:

¡¤Linuxϵͳ¼ò½é (2025-12-25 21:55:25)
¡¤Linux°²×°MySQL¹ý³Ì (2025-12-25 21:55:22)
¡¤Linuxϵͳ°²×°½Ì³Ì£¨ (2025-12-25 21:55:20)
¡¤HTTP Åc HTTPS µÄ²î„ (2025-12-25 21:19:45)
¡¤ÍøÕ¾°²È«±ØÐ޿ΣºÍ¼ (2025-12-25 21:19:42)