我是靠谱客的博主 友好猫咪,这篇文章主要介绍【剑指offer】JZ79 判断是不是平衡二叉树,现在分享给大家,希望可以做个参考。

1. 题目

JZ79 判断是不是平衡二叉树

2. 代码实现

/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function IsBalanced_Solution(pRoot)
{
// write code here
if(pRoot === null) return true
const left = getTreeDepth(pRoot.left)
const right = getTreeDepth(pRoot.right)
if(Math.abs(left - right) > 1) {
return false
}
const leftTree = IsBalanced_Solution(pRoot.left)
const rightTree = IsBalanced_Solution(pRoot.right)
return leftTree && rightTree
}
function getTreeDepth(root) {
if(root === null) return 0
const left = getTreeDepth(root.left)
const right = getTreeDepth(root.right)
return Math.max(left, right) + 1
}
module.exports = {
IsBalanced_Solution : IsBalanced_Solution
};

最后

以上就是友好猫咪最近收集整理的关于【剑指offer】JZ79 判断是不是平衡二叉树的全部内容,更多相关【剑指offer】JZ79内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(114)

评论列表共有 0 条评论

立即
投稿
返回
顶部