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 diffe

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;
        int left=treeDepth(root.left);
        int right=treeDepth(root.right);
        if(Math.abs(left-right)<=1){//条件
            if(isBalanced(root.left)&&isBalanced(root.right))//递归调用isBanlianced(root);
                return true;
        }
        return false;

    }

    private int treeDepth(TreeNode root) {//求树的深度
        // TODO Auto-generated method stub
        if(root==null)return 0;

        return Math.max(treeDepth(root.right), treeDepth(root.left))+1;
    }
}
时间: 2024-08-23 23:16:50

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 diffe的相关文章

[Daily Coding Problem 223] O(1) space in order traversal of a binary tree

Typically, an implementation of in-order traversal of a binary tree has O(h) space complexity, where h is the height of the tree. Write a program to compute the in-order traversal of a binary tree using O(1) space. In-order traversal without recursio

【Leetcode】【Easy】Balanced Binary Tree

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. 错误的思路(o(N2)时间复杂度): 写一个函数a,用递归遍历的方法,

Balanced Binary Tree(平衡二叉树)

来源:https://leetcode.com/problems/balanced-binary-tree 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 diff

Balanced Binary Tree

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

LeetCode110 Balanced Binary Tree

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.(Easy) 分析: 利用辅助函数求深度,然后根据heighted-ba

Leetcode[110]-Balanced Binary Tree

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. 判断一棵树是否属于平衡二叉树 判断主节点的左右节点深度大小差,如果不在

leetcode之Maximum Depth of Binary Tree 以及Balanced Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 二叉树的深度,用递归特别方便! 代码如下: public int maxDepth(TreeNode root) { if(root==null){ return 0; } int

[LeetCode]Balanced Binary Tree

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. 这道题以前整理过,这里就不细写了,直接贴上代码: /** * Defi

LeetCode OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined