forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
879 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,71 @@ | ||
/** | ||
* 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) | ||
* } | ||
* https://leetcode.com/problems/same-tree/ | ||
* TIme O(N) | Space O(H) | ||
* @param {TreeNode} p | ||
* @param {TreeNode} q | ||
* @return {boolean} | ||
*/ | ||
var isSameTree = function(p, q) { | ||
const isBaseCase = !(p || q); | ||
if (isBaseCase) return true; | ||
|
||
const isBalanced = (p && q); | ||
if (!isBalanced) return false; | ||
|
||
const isSame = p.val === q.val; | ||
if (!isSame) return false; | ||
|
||
return dfs(p, q); | ||
}; | ||
|
||
const dfs = (p, q) => { | ||
const left = isSameTree(p.left, q.left); | ||
const right = isSameTree(p.right, q.right); | ||
|
||
return left && right; | ||
} | ||
|
||
/** | ||
* https://leetcode.com/problems/same-tree/ | ||
* TIme O(N) | Space O(W) | ||
* @param {TreeNode} p | ||
* @param {TreeNode} q | ||
* @return {boolean} | ||
*/ | ||
var isSameTree = function (p, q) { | ||
if (!p && !q) return true; | ||
if (!p || !q || p.val !== q.val) return false; | ||
return isSameTree(p.right, q.right) && isSameTree(p.left, q.left); | ||
}; | ||
var isSameTree = function(p, q) { | ||
if (isSameNode(p, q)) return true; | ||
|
||
return bfs([[ p, q ]]); | ||
} | ||
|
||
const bfs = (queue) => { | ||
while (queue.length) { | ||
for (let i = (queue.length - 1); 0 <= i; i--) { | ||
const [ p, q ] = queue.shift(); | ||
|
||
if (!isSame(p, q)) return false; | ||
|
||
if (p.left) queue.push([ p.left, q.left ]); | ||
if (p.right) queue.push([ p.right, q.right ]); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
const isSameNode = (p, q) => { | ||
const isBaseCase = !(p || q); | ||
if (isBaseCase) return true; | ||
|
||
const isBalanced = (p && q); | ||
if (!isBalanced) return false; | ||
|
||
const isSame = p.val === q.val; | ||
if (!isSame) return false; | ||
|
||
return true; | ||
} | ||
|
||
const isSame = (p, q) => isSameNode(p, q) | ||
&& isSameNode(p.left, q.left) | ||
&& isSameNode(p.right, q.right); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,56 @@ | ||
/** | ||
* 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) | ||
* } | ||
*/ | ||
/** | ||
* https://leetcode.com/problems/binary-tree-level-order-traversal/ | ||
* Time O(N) | Space O(W) | ||
* @param {TreeNode} root | ||
* @return {number[][]} | ||
*/ | ||
var levelOrder = function (root) { | ||
if (!root) return []; | ||
var levelOrder = function(root) { | ||
const isBaseCase = root === null; | ||
if (isBaseCase) return []; | ||
|
||
const result = []; | ||
const queue = [root]; | ||
return bfs([ root ]); | ||
}; | ||
|
||
const bfs = (queue, levels = []) => { | ||
while (queue.length) { | ||
const numNodes = queue.length; | ||
const temp = []; | ||
for (let i = 0; i < numNodes; i++) { | ||
const subtree = queue.shift(); | ||
temp.push(subtree.val); | ||
if (subtree.left !== null) queue.push(subtree.left); | ||
if (subtree.right !== null) queue.push(subtree.right); | ||
const level = []; | ||
|
||
for (let i = (queue.length - 1); 0 <= i; i--) { | ||
const node = queue.shift(); | ||
|
||
if (node.left) queue.push(node.left); | ||
if (node.right) queue.push(node.right); | ||
|
||
level.push(node.val); | ||
} | ||
result.push(temp); | ||
|
||
levels.push(level.slice()); | ||
} | ||
|
||
return result; | ||
}; | ||
return levels; | ||
} | ||
|
||
/** | ||
* https://leetcode.com/problems/binary-tree-level-order-traversal/ | ||
* Time O(N) | Space O(H) | ||
* @param {TreeNode} root | ||
* @return {number[]} | ||
*/ | ||
var levelOrder = function(root, level = 0, levels = []) { | ||
const isBaseCase = root === null; | ||
if (isBaseCase) return levels; | ||
|
||
const isLastNode = level === levels.length; | ||
if (isLastNode) levels.push([]); | ||
|
||
levels[level].push(root.val); | ||
|
||
return dfs(root, level, levels); | ||
} | ||
|
||
const dfs = (root, level, levels) => { | ||
if (root.left) levelOrder(root.left, (level + 1), levels); | ||
if (root.right) levelOrder(root.right, (level + 1), levels); | ||
|
||
return levels; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,50 @@ | ||
/** | ||
* 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) | ||
* } | ||
* https://leetcode.com/problems/maximum-depth-of-binary-tree/ | ||
* TIme O(N) | Space O(N) | ||
* @param {TreeNode} root | ||
* @return {number} | ||
*/ | ||
var maxDepth = function(root) { | ||
const isBaseCase = root === null; | ||
if (isBaseCase) return 0; | ||
|
||
return dfs(root); | ||
}; | ||
|
||
const dfs = (root) => { | ||
const left = maxDepth(root.left); | ||
const right = maxDepth(root.right); | ||
|
||
const height = Math.max(left, right); | ||
|
||
return height + 1; | ||
} | ||
|
||
/** | ||
* https://leetcode.com/problems/maximum-depth-of-binary-tree/ | ||
* TIme O(N) | Space O(N) | ||
* @param {TreeNode} root | ||
* @return {number} | ||
*/ | ||
var maxDepth = (root) => { | ||
let maxDepth = 0; | ||
let DFS = (node, depth) => { | ||
if (!node) return maxDepth; | ||
if (depth > maxDepth) maxDepth = depth; | ||
DFS(node.right, depth + 1); | ||
DFS(node.left, depth + 1); | ||
}; | ||
DFS(root, 1); | ||
return maxDepth; | ||
}; | ||
var maxDepth = function(root) { | ||
const isBaseCase = root === null; | ||
if (isBaseCase) return 0; | ||
|
||
return bfs([[ root, 0 ]]); | ||
} | ||
|
||
const bfs = (queue, height = 0) => { | ||
while (queue.length) { | ||
for (let i = (queue.length - 1); 0 <= i; i--) { | ||
const [ root, depth ] = queue.shift(); | ||
|
||
height = Math.max(height, (depth + 1)); | ||
|
||
if (root.left) queue.push([ root.left, (depth + 1) ]); | ||
if (root.right) queue.push([ root.right, (depth + 1) ]); | ||
} | ||
} | ||
|
||
return height; | ||
} | ||
|
72 changes: 62 additions & 10 deletions
72
javascript/105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,62 @@ | ||
function buildTree(preorder, inorder) { | ||
if (!preorder.length || !inorder.length) return null; | ||
|
||
let root = new TreeNode(preorder[0]); | ||
let mid = inorder.indexOf(preorder[0]); | ||
|
||
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid)); | ||
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1)); | ||
return root; | ||
} | ||
/** | ||
* https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ | ||
* Time O(N^2) | Space(H) | ||
* @param {number[]} preorder | ||
* @param {number[]} inorder | ||
* @return {TreeNode} | ||
*/ | ||
var buildTree = function(preorder, inorder) { | ||
const isBaseCase = !preorder.length || !inorder.length; | ||
if (isBaseCase) return null; | ||
|
||
return dfs(preorder, inorder); | ||
} | ||
|
||
var dfs = (preorder, inorder) => { | ||
const { leftInorder, mid, rightInorder } = getPointers(preorder, inorder); | ||
const root = new TreeNode(inorder[mid]); | ||
|
||
root.left = buildTree(preorder, leftInorder); | ||
root.right = buildTree(preorder, rightInorder); | ||
|
||
return root; | ||
} | ||
|
||
const getPointers = (preorder, inorder) => { | ||
const next = preorder.shift(); | ||
const mid = inorder.indexOf(next); | ||
const leftInorder = inorder.slice(0, mid); | ||
const rightInorder = inorder.slice(mid + 1); | ||
|
||
return { leftInorder, mid, rightInorder }; | ||
} | ||
|
||
/** | ||
* https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ | ||
* Time O(N) | Space(H) | ||
* @param {number[]} preorder | ||
* @param {number[]} inorder | ||
* @return {TreeNode} | ||
*/ | ||
var buildTree = function(preorder, inorder, max = -Infinity, indices = { preorder: 0, inorder: 0 }) { | ||
const isBaseCase = preorder.length <= indices.inorder; | ||
if (isBaseCase) return null; | ||
|
||
const isAtEnd = inorder[indices.inorder] === max; | ||
if (isAtEnd) { | ||
indices.inorder++; | ||
return null; | ||
} | ||
|
||
return dfs(preorder, inorder, max, indices); | ||
} | ||
|
||
var dfs = (preorder, inorder, max, indices) => { | ||
const val = preorder[indices.preorder++] | ||
const root = new TreeNode(val); | ||
|
||
root.left = buildTree(preorder, inorder, root.val, indices); | ||
root.right = buildTree(preorder, inorder, max, indices); | ||
|
||
return root; | ||
} |
Oops, something went wrong.