Created
November 17, 2019 09:53
-
-
Save criskgl/e934d72d01f7d4ecaff21e6452c54664 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
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