本文主要是介绍力扣110. 平衡二叉树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
给定一个二叉树,判断它是否是
平衡二叉树
示例 1:
输入:root = [3,9,20,null,null,15,7] 输出:true
示例 2:
输入:root = [1,2,2,3,3,null,null,4,4] 输出:false
示例 3:
输入:root = [] 输出:true
/*** Definition for a binary tree node.* function TreeNode(val, left, right) {* this.val = (val===undefined ? 0 : val)* this.left = (left===undefined ? null : left)* this.right = (right===undefined ? null : right)* }*/
/*** @param {TreeNode} root* @return {boolean}*/
var isBalanced = function (root) {function getHight(root) {if (!root) {return 0}let left = getHight(root.left)if(left==-1) return -1let right = getHight(root.right)if (right==-1||Math.abs(left - right) > 1) return -1return Math.max(left, right) + 1}return getHight(root) != -1 ? true : false};
这篇关于力扣110. 平衡二叉树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!