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
| /* | |
| List of depths: Given a binary tree, design an algorithm which creates a linked list of all the nodes | |
| at each depth (eg if you have a tree with depth D, you'll have D linked lists.) | |
| */ | |
| interface DepthQueue<T> { | |
| depth: number, | |
| node: T | |
| } |
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
| /* | |
| Check balanced: Implement a function to check if a binary tree is balanced. For the purposes of | |
| this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any | |
| node never differ by more than one. | |
| */ | |
| // const myTree = buildTree([1,2,3,4,5,6,7,8, 9, 10, 11, 12, 13, 14]) | |
| const myTree = new BinaryNode(1) | |
| myTree.rightChild = new BinaryNode(2) | |
| myTree.rightChild.rightChild = new BinaryNode(3) |
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
| // Valiate binary tree: Implement a function to validate that a binary tree is a binary search tree. | |
| function validateBST<T>(node: BinaryNode<T>) { | |
| if (!node) return true | |
| if (node.leftChild && node.leftChild.data > node.data) return false | |
| if (node.rightChild && node.rightChild.data < node.data) return false | |
| return validateBST(node.leftChild) && validateBST(node.rightChild) | |
| } |
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
| /* | |
| Developer notes: | |
| Written in Typescript. | |
| I chose to use an iterative approach because it's able to handle more data than a recursive approach | |
| before hitting a stackoverfow error. | |
| This algorithm runs in O(N) time and O(N) space, while avoiding cycles without actively searching for them. | |
| Actively checking for cycles would make the time compexity O(NlogN). |
OlderNewer