This file contains hidden or 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
| var createMinimalBST = function(array){ | |
| var makeNode = function(value){ | |
| var obj = { | |
| value:value, | |
| left:null, | |
| right:null | |
| }; | |
| return obj; | |
| }; | |
This file contains hidden or 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
| var findLevelLinkedList = function(root){ | |
| var queue = []; | |
| var linkedListArray = []; | |
| var level = 0; | |
| queue.push([root, level]); | |
| root.visit = true; | |
| while(queue.length !== 0){ |
This file contains hidden or 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
| var inOrderSucc = function(node){ | |
| var p = null; | |
| if (node !== null){ | |
| if (node.parent === null || node.right !== null){ | |
| p = leftMostChild(node.right); | |
| console.log("result=", p); | |
| } else { | |
| while ((p = node.parent) !== null){ | |
| if (p.left === node){ |
This file contains hidden or 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
| function mergeSort(array, start, end){ | |
| if (start === end) return [array[start]]; | |
| var mid = Math.floor( (start + end) / 2 ); | |
| var arr1 = mergeSort(array, start, mid); | |
| var arr2 = mergeSort(array, mid+1, end); | |
| var result = merge(arr1, arr2); | |
This file contains hidden or 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
| //1,2,3,4,4,4,4,4,5,6,7 | |
| function findFrequency(array, target){ | |
| var first, last; | |
| var result = -1; | |
| first = findFirst(array, target, 0, array.length-1); | |
| if (first !== -1){ | |
| last = findLast(array, target, 0, array.length-1); |
This file contains hidden or 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
| function checkBST(node){ | |
| lastVisited = null; | |
| function checkBSTRecurse (node){ | |
| if (node === null) return true; | |
| if ( !checkBSTRecurse(node.left) ) return false; | |
| if (node.val < lastVisited) return false; |
This file contains hidden or 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
| function containTrees(tree1, tree2){ | |
| if (tree2 === null){ | |
| return true; | |
| } | |
| return subTree(tree1, tree2); | |
| } | |
| function subTree(tree1, tree2){ |
This file contains hidden or 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
| function createMinimalBST(array, start, end){ | |
| if (end < start){ | |
| return null; | |
| } | |
| var mid = Math.floor( (start + end) / 2 ); | |
| var node = {val: array[mid], left: null, right: null}; | |
| node.left = createMinimalBST(array, start, mid-1); | |
| node.right = createMinimalBST(array, mid+1, end); |
This file contains hidden or 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
| function covers(root, node){ | |
| if (root === null ) return false; | |
| if (root === node ) return true; | |
| return covers(root.left, node) || covers(root.right, node); | |
| } | |
| function commonAncestorHelper(root, node1, node2){ | |
| if (root === null ) return false; | |
| if (root === node1 || root === node2 ) return root; | |
This file contains hidden or 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
| function quickSort(array, start, end){ | |
| var leftIndex = partition(array, start, end); | |
| if (start < leftIndex-1){ | |
| quickSort(array, start, leftIndex-1); | |
| } | |
| if (start > leftIndex){ | |
| quickSort(array, leftIndex, end); |