Created
April 20, 2020 23:09
-
-
Save IngeFrodo/ddf7c5a33f8844cd60b81f031479963b to your computer and use it in GitHub Desktop.
This file contains 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
class BSTFromPreOrder { | |
public TreeNode bstFromPreorder(int[] preorder) { | |
TreeNode root = null; | |
for (int val : preorder) { | |
root = insert(root, val); | |
} | |
return root; | |
} | |
private TreeNode insert(TreeNode node, int val) { | |
if (node == null) { | |
return new TreeNode(val); | |
} | |
if (val < node.val) { | |
node.left = insert(node.left, val); | |
} else { | |
node.right = insert(node.right, val); | |
} | |
return node; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment