zoukankan      html  css  js  c++  java
  • [Leetcode 9] 110 Balanced Binary Tree

    Problem:

    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 everynode never differ by more than 1.

    Analysis:

    This problem is different from the 4.1 problem in Cracking the Code Interview. Their definitions of "Balanced" are different. In the CCI  problem, the definition is “a tree is balanced such that no two leaf nodes differ in distance from the root by more than one“. But here the definition is "the depth of the two subtrees of everynode never differ by more than 1".

    For a example, the following binary tree is balanced under the second definition but not balanced under the first definition:

                      1

               2             2

           3     3       3      

        4   4  4  4 

    Due to the intrinsically recursion of a tree, we can solve the problem in a resursive way. First if the root is null, then it's balanced; else if its two subtree's height differ more than one, it's not balanced; else return if its left and right subtree is balanced

    Code:

    View Code

    Here is a more efficient version with early exit

    View Code
     1 /**
     2  * Definition for binary tree
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public boolean isBalanced(TreeNode root) {
    12         // Start typing your Java solution below
    13         // DO NOT write main() function
    14         return height(root) != -1;
    15     }
    16     
    17     
    18     private int height (TreeNode node) {
    19         if (node == null) return 0;
    20         
    21         int lh = height(node.left);
    22         if (lh == -1)
    23             return -1;
    24             
    25         int rh = height(node.right);
    26         if (rh == -1)
    27             return -1;
    28         
    29         int height_dif = diff (lh, rh);
    30         if (height_dif > 1)
    31             return -1;
    32         else
    33             return 1 + max(lh, rh);
    34     }
    35     
    36     
    37     private int diff(int a, int b) {
    38         return (a>b) ? (a-b) : (b-a);
    39     }
    40     
    41     private int max(int a, int b) {
    42         return (a>b) ? a : b;
    43     }
    44     
    45 }

    Attention:

    The check balanced function and the height function are some kind of redundant, try to find a way that combine them together thus leaving only one parse of the given binary tree.

  • 相关阅读:
    linux上部署docker+tomcat服务,并部署项目
    docker使用Dockerfile把springboot项目jar包生成镜像并运行
    springboot配置log4j
    mysql常用函数
    java处理csv文件上传示例
    中国城市区号脚本-mysql
    java微信公众号支付示例
    java导出csv格式文件
    mysql时间相加函数DATE_ADD()
    centos分区
  • 原文地址:https://www.cnblogs.com/freeneng/p/3011657.html
Copyright © 2011-2022 走看看