Problem Balanced Binary Tree

Problem Description:

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.

Solution:

 1     public boolean isBalanced(TreeNode root) {
 2         int[] height = new int[1];
 3         return isBalancedWithHeight(root, height);
 4     }
 5
 6     public boolean isBalancedWithHeight(TreeNode root, int[] height) {
 7 int[] lh = new int[1];
 8         int[] rh = new int[1];
 9         boolean l,r;
10
11         if (root == null) {
12             height[0] = 0;
13             return true;
14         }
15
16         l = isBalancedWithHeight(root.left, lh);
17         r = isBalancedWithHeight(root.right, rh);
18
19         height[0] = ((lh[0] > rh[0])? lh[0] : rh[0]) + 1;
20
21         return (Math.abs(lh[0]-rh[0]) < 2) && l && r;
22     }    

Problem Balanced Binary Tree

时间: 2024-12-07 17:15:04

Problem Balanced Binary Tree的相关文章

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

LeetCode: Balanced Binary Tree 解题报告

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. Show Tags SOLU

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

题目链接: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][JavaScript]Balanced Binary Tree

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. https://leetco

[LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

Problem 1 [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. Pr

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. 中文:给定一棵二叉树,检测其高度上平衡否. 于此题,一棵高度上平衡的二

[Lintcode] Balanced Binary Tree

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. Example Given

leetcode_110_ Balanced Binary Tree

欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢  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 m