Back to all solutions
#1305 - All Elements in Two Binary Search Trees
Problem Description
Given two binary search trees root1 and root2, return a list containing all the integers from both trees sorted in ascending order.
Solution
/**
* 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)
* }
*/
/**
* @param {TreeNode} root1
* @param {TreeNode} root2
* @return {number[]}
*/
var getAllElements = function(root1, root2) {
const values = [];
inorder(root1);
inorder(root2);
return values.sort((a, b) => a - b);
function inorder(node) {
if (!node) return;
inorder(node.left);
values.push(node.val);
inorder(node.right);
}
};