[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://leetcode.com/problems/balanced-binary-tree/



判断给的数是不是平衡二叉树,平衡的定义是指任意节点的左右子树高度相差不超过1。

一个递归方法,返回树的高度,如果左右子树高度相差超过1则返回-1。

 1 /**
 2  * Definition for a binary tree node.
 3  * function TreeNode(val) {
 4  *     this.val = val;
 5  *     this.left = this.right = null;
 6  * }
 7  */
 8 /**
 9  * @param {TreeNode} root
10  * @return {boolean}
11  */
12 var isBalanced = function(root) {
13     return treeHeight(root) === -1 ? false : true;
14
15     /**
16      * @return {int} tree‘s height
17      * return -1 if depth differ of the two subtrees are more than 1
18      */
19     function treeHeight(node){
20         if(node === null) return 0;
21         if(node.left === null && node.right === null) return 1;
22         var leftH = treeHeight(node.left);
23         var rightH = treeHeight(node.right);
24         if(leftH === -1 || rightH === -1) return -1;
25         if(Math.abs(leftH - rightH) > 1) return -1;
26         return Math.max(leftH, rightH) + 1;
27     }
28 };
时间: 2024-08-07 21:18:21

[LeetCode][JavaScript]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 -day18 Balanced Binary Tree

测试源码: //测试派生类的构造函数的调用顺序何时调用 //Fedora20 gcc version=4.8.2 #include <iostream> using namespace std; class base { public: base() { cout<<"base created!"<<endl; } ~base() { cout<<"base destroyed!"<<endl; } };

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][Java] 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 平衡二叉树

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题目: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. tags:深度优先遍历 c

【leetcode】Balanced Binary Tree(middle)

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][JavaScript]Invert Binary Tree

Invert Binary Tree Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a

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