From ff75f25f66fba21735ffd12b63904e8c4211c1eb Mon Sep 17 00:00:00 2001 From: Aerex Date: Tue, 31 Mar 2020 00:22:29 -0500 Subject: [PATCH] feat: Added implementation for all elements in tow binary search tree in nodejs --- .../nodejs/index.js | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 all-elements-in-two-binary-search-tree/nodejs/index.js diff --git a/all-elements-in-two-binary-search-tree/nodejs/index.js b/all-elements-in-two-binary-search-tree/nodejs/index.js new file mode 100644 index 0000000..de1af2e --- /dev/null +++ b/all-elements-in-two-binary-search-tree/nodejs/index.js @@ -0,0 +1,139 @@ +/* + * + * Given two binary search trees root1 and root2. + * Return a list containing all the integers from both trees sorted in ascending order. + * + * + * Example 1: + * Input: root1 = [2,1,4], root2 = [1,0,3] + * Output: [0,1,1,2,3,4] + * + * Example 2: + * Input: root1 = [0,-10,10], root2 = [5,1,7,0,2] + * Output: [-10,0,0,1,2,5,7,10] + * + * Example 3: + * Input: root1 = [], root2 = [5,1,7,0,2] + * Output: [0,1,2,5,7] + * + * Example 4: + * Input: root1 = [0,-10,10], root2 = [] + * Output: [-10,0,10] + * + * Example 5: + * Input: root1 = [1,null,8], root2 = [8,1] + * Output: [1,1,8,8] + * + * Constraints: + * Each tree has at most 5000 nodes. + * Each node's value is between [-10^5, 10^5]. + * + */ +const { assert } = require('../../util/js'); + +/** + * Definition for a binary tree node. + */ +function TreeNode(val) { + this.val = val; + this.left = this.right = null; +} +/** + * @param {TreeNode} root1 + * @param {TreeNode} root2 + * @return {number[]} + */ +const getAllElements = function(root1, root2) { + + const convertToList = (node, list) => { + if(!node) { + return; + } + + convertToList(node.left, list); + list.push(node.val); + convertToList(node.right, list); + } + + let listA = []; + let listB = []; + const result = []; + convertToList(root1, listA); + convertToList(root2, listB); + + let i = 0; + let j = 0; + while(i < listA.length && j < listB.length) { + if (listA[i] < listB[j]) { + result.push(listA[i]); + i++; + } else { + result.push(listB[j]); + j++; + } + } + + while(i < listA.length) { + result.push(listA[i]); + i++; + } + + while(j < listB.length){ + result.push(listB[j]); + j++; + } + + return result; +}; + +/* Test Case 1 + * Input: root1 = [2,1,4], root2 = [1,0,3] + * Output: [0,1,1,2,3,4] + */ +root1 = new TreeNode(2); +root1.left = new TreeNode(1); +root1.right = new TreeNode(4); +root2 = new TreeNode(1); +root2.left = new TreeNode(0); +root2.right = new TreeNode(3); +sol = [0,1,1,2,3,4]; +assert(JSON.stringify(getAllElements(root1,root2)) === JSON.stringify(sol), 'Output: [0,1,1,2,3,4]'); + +/* Test Case 2 + * Input: root1 = [0,-10,10], root2 = [5,1,7,0,2] + * Output: [-10,0,0,1,2,5,7,10] + */ +root1 = new TreeNode(0); +root1.left = new TreeNode(-10); +root1.right = new TreeNode(10); +root2 = new TreeNode(5); +root2.left = new TreeNode(1); +root2.right = new TreeNode(7); +root2.left.left = new TreeNode(0); +root2.left.right = new TreeNode(2); +sol = [-10,0,0,1,2,5,7,10]; +assert(JSON.stringify(getAllElements(root1, root2)) === JSON.stringify(sol), 'Output: [-10,0,0,1,2,5,7,10]'); + +/* Test Case 3 + * Input: root1 = [], root2 = [5,1,7,0,2] + * Output: [0,1,2,5,7] + */ +root1 = undefined; +root2 = new TreeNode(5); +root2.left = new TreeNode(1); +root2.right = new TreeNode(7); +root2.left.left = new TreeNode(0); +root2.left.right = new TreeNode(2); +sol = [0,1,2,5,7]; +assert(JSON.stringify(getAllElements(root1,root2)) === JSON.stringify(sol), 'Output: [0,1,2,5,7]'); + +/* Test Case 4 + * Input: root1 = [0,-10,10], root2 = [] + * Output: [-10,0,10] + */ +root1 = new TreeNode(0); +root1.left = new TreeNode(-10); +root1.right = new TreeNode(10); +root2 = undefined; +sol = [-10,0,10]; +assert(JSON.stringify(getAllElements(root1, root2)) === JSON.stringify(sol), 'Output: [-10,0,10]');