leetcode 110. Balanced Binary Tree

转载 2016年05月31日 10:28:43

题目

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.

/**
 * Definition for a binary tree node.
 * public 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(Math.abs(maxDepth(root.left)- maxDepth(root.right))>1){
            return false;
        }

        return isBalanced(root.left)&&isBalanced(root.right);                       
    }

    public  int maxDepth(TreeNode root){
        if(root==null) 
            return 0;


        return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
    }
}

相关文章推荐

剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=...

[leetcode]: 110. Balanced Binary Tree

1.题目Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced bina...

LeetCode110.Balanced Binary Tree题解

1. 题目描述Given a binary tree, determine if it is height-balanced. For this problem, a height-balanc...
  • lby0910
  • lby0910
  • 2017年03月09日 19:31
  • 149

【LeetCode-面试算法经典-Java实现】【110-Balanced Binary Tree(平衡二叉树)】

【110-Balanced Binary Tree(平衡二叉树)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree, determin...

LeetCode 110. Balanced Binary Tree

1. 题目描述 Given a binary tree, determine if it is height-balanced. For this problem, a height-b...

LeetCode(110)Balanced Binary Tree

题目如下: 分析如下: 之前尝试一边计算深度,一边做判断,发现这是无法实现的。因为bool型的返回变量说明了返回值没有深度信息,而如果没有深度信息,那么就无法判断左子树和右子树的深度差是否为1.所...

<LeetCode OJ> 110. Balanced Binary Tree

110. Balanced Binary Tree Total Accepted: 100175 Total Submissions: 298232 Difficulty: Easy G...

LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)

翻译给定一个二叉树,决定它是否是高度平衡的。(高度是名词不是形容词……对于这个问题,一个高度平衡二叉树被定义为:这棵树的每个节点的两个子树的深度差不能超过1。原文Given a binary tree...
  • NoMasp
  • NoMasp
  • 2016年01月21日 11:46
  • 3104

【leetcode】110. Balanced Binary Tree

一、题目描述 Given a binary tree, determine if it is height-balanced. For this problem, a height-b...

[leetcode] 110.Balanced Binary Tree

题目: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced bin...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 110. Balanced Binary Tree
举报原因:
原因补充:

(最多只允许输入30个字)