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.

思路:判断是否是平衡二叉树,dfs即可,递归判断每个子树是否平衡二叉树,如果有子树不满足,则整体也不满足。

具体代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    /**
     * 判断是否平衡二叉树
     * 看左右子树高度差是否超过1
     * 不超过1则分别判断左右子树是否平衡二叉树
     */
    public boolean isBalanced(TreeNode root) {
        if(root == null)
            return true;
        if(dep(0,root) > - 10)
        	return true;
        return false;
    }
    //计算树的高度
    private int dep(int dep,TreeNode root){
        if(root == null){
            return dep-1;
        }
        int dep1 = dep(dep+1,root.left);
        int dep2 = dep(dep+1,root.right);
        //如果高度差超过1,返回-10
        //则以后的dep返回值均为-10
        if(Math.abs(dep1 - dep2) > 1){
        	return -10;
        }
        return dep1 > dep2 ? dep1 : dep2;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 15:08:25

leetCode 110.Balanced Binary Tree (平衡二叉树) 解题思路和方法的相关文章

leetCode 110. Balanced Binary Tree 平衡二叉树

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 110. Balanced Binary Tree 递归求解

题目链接:https://leetcode.com/problems/balanced-binary-tree/ 110. Balanced Binary Tree My Submissions Question Total Accepted: 97926 Total Submissions: 292400 Difficulty: Easy Given a binary tree, determine if it is height-balanced. For this problem, a h

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

翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 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

Java for 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. 解题思路: 递归即可,JAVA实现如下: public boolean

Java [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. 解题思路: 递归法解题. 代码如下: /** * Defi

110 Balanced Binary Tree 平衡二叉树

给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过 1.案例 1:给出二叉树 [3,9,20,null,null,15,7]:    3   / \  9  20    /  \   15   7返回 true .案例 2:给出二叉树 [1,2,2,3,3,null,null,4,4]:       1      / \     2   2    / \   3   3  / \ 4   4返回 false .详见:ht

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 110 Balanced Binary Tree ----- java

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. 判断一棵树是否是平衡二叉树 利用高度的答案,递归求解. /** * D

leetCode 67.Add Binary (二进制加法) 解题思路和方法

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 思路:二进制加法,比较简单.代码如下: public class Solution { public String addBinary(String a, String b) { int len = Math.max(a.len