Skip to content

Commit

Permalink
refactor: js - tree
Browse files Browse the repository at this point in the history
  • Loading branch information
aakhtar3 committed Sep 19, 2022
1 parent 9897759 commit 2275130
Show file tree
Hide file tree
Showing 16 changed files with 879 additions and 324 deletions.
75 changes: 64 additions & 11 deletions javascript/100-Same-Tree.js
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);
68 changes: 46 additions & 22 deletions javascript/102-Binary-Tree-Level-Order-Traversal.js
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;
}
61 changes: 44 additions & 17 deletions javascript/104-Maximum-Depth-of-Binary-Tree.js
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;
}

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;
}
Loading

0 comments on commit 2275130

Please sign in to comment.