This is No.148 Problem in LeetCode. Here are two approaches as below: Top Down Merge Sort Recursively split the original list into two halves. The split continues until there is only one node in the linked list (Divide phase). To split the list into two halves, we find the middle of the linked list using …
How to construct a BST with a given pre-order array?
Code is below: // build the tree private TreeNode build_tree( String[] node, int min, int max ){ // invalid corner case if( idx >= node.length ){ return null; } TreeNode root = null; // valid value if( Integer.valueOf(node[idx]) > min && Integer.valueOf(node[idx]) < max ){ // build the root root = new TreeNode( Integer.valueOf( node[idx]) …
Continue reading “How to construct a BST with a given pre-order array?”