判断是否是平衡二叉树
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null; } */ function IsBalanced_Solution(pRoot) { // write code here if(!pRoot) return true function dfs(root){ if(!root) return 0 return Math.max(dfs(root.left),dfs(root.right))+1 } let left = dfs(pRoot.left) let right = dfs(pRoot.right) if(left-right>1 || right-left >1) return false return true } module.exports = { IsBalanced_Solution : IsBalanced_Solution };
树算法 文章被收录于专栏
树相关算法