Skip to content

Instantly share code, notes, and snippets.

@criskgl
Created November 17, 2019 09:53
Show Gist options
  • Save criskgl/e934d72d01f7d4ecaff21e6452c54664 to your computer and use it in GitHub Desktop.
Save criskgl/e934d72d01f7d4ecaff21e6452c54664 to your computer and use it in GitHub Desktop.
public TreeNode insertIntoBST(TreeNode root, int val) {
TreeNode tNode = new TreeNode(val);
TreeNode current = root;
while(true){
if(val > current.val){
//BIGGER
if(current.right == null){//insert
current.right = tNode;
break;
}
current = current.right;
}else{
//SMALLER
if(current.left == null){//insert
current.left = tNode;
break;
}
current = current.left;
}
}
return root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment