GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — 平衡二叉树-110 · Issue #56 · sl1673495/leetcode-javascript Open Description给定一个二叉树,判断它是否是高度平衡的二叉树。 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。 示例 1: 来源:力扣(LeetCode) 思路首先定义一个辅助函数 function getHeight(node) { if (!node) return 0 return Math.max(getHeight(node.left), getHeight(node.right)) + 1 } 之后是否平衡就比较好求了,首先比较直接子节点 var isBalanced = function (root) { if (!root) { return true } let isSonBalnaced = Math.abs(getHeight(root.left) - getHeight(root.right)) <= 1 return isSonBalnaced && isBalanced(root.left) && isBalanced(root.right) }; Activity |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |