Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions leet-code/110-balanaced-binary-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 110-balanaced-binary-tree

Given a binary tree, determine if it is height-balanced.

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2:

Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3:

Input: root = []
Output: true

Constraints:

The number of nodes in the tree is in the range [0, 5000].
-104 <= Node.val <= 104
82 changes: 82 additions & 0 deletions leet-code/110-balanaced-binary-tree/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* 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) {
// store post-order traversal
let stack = []
// store the current node
let current = root;
// track the most recently processed node
let lastVisited = null
// map the hight of subtree by node
let heights = new Map()
// continue visiting nodes until we've exhasuted the tree or found unbalanced structure
while (stack.length > 0 || current) {
// Visit as far left as possible
if (current) {
stack.push(current)
current = current.left
continue;
}
// peek the top of stack
const top = stack[stack.length-1]
// visit the right subtree
if (top.right && lastVisited !== top.right) {
current = top.right
continue
}
// We've visisted all children of top element
stack.pop()
// Get the height of the left and right subtree
const leftH = heights.get(top.left) || 0
const rightH = heights.get(top.right) || 0
// short circuit if subtree is unbalanaced
if (Math.abs(leftH - rightH) > 1) return false;
// record the height of the current node's subtree
heights.set(top, Math.max(leftH, rightH) + 1)
// Mark top as the last one evaluated
lastVisited = top;
}
// No subtrees were found to be unbalanaced
return 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 isBalancedRecursive = function(root) {
return maxDepth(root) >= 0;
};

var maxDepth = function(root) {
if(root==null) return 0;

let left= maxDepth(root.left);
let right= maxDepth(root.right);

if(left === -1 || right === -1) return -1;

if(Math.abs(left-right)>1) return -1;

return (Math.max(left,right) + 1);
};