File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public TreeNode insertIntoBST (TreeNode root , int val ) {
3
+ if (root == null ) return new TreeNode (val );
4
+ TreeNode curr = root ;
5
+ while (true ){
6
+ if (curr .val <= val ){
7
+ if (curr .right != null ){
8
+ curr = curr .right ;
9
+ }else {
10
+ curr .right = new TreeNode (val );
11
+ break ;
12
+ }
13
+ }else {
14
+ if (curr .left != null ) curr = curr .left ;
15
+ else {
16
+ curr .left = new TreeNode (val );
17
+ break ;
18
+ }
19
+ }
20
+ }
21
+ return root ;
22
+ }
23
+
24
+ /* Using Recursive Solution
25
+ -------------------------------------------------------------------
26
+ public TreeNode insertIntoBST(TreeNode root, int val) {
27
+ if(root == null) return new TreeNode(val);
28
+ if(root.val <= val){
29
+ root.right = insertIntoBST(root.right, val);
30
+ }else{
31
+ root.left = insertIntoBST(root.left, val);
32
+ }
33
+ return root;
34
+ }
35
+ -------------------------------------------------------------------
36
+ */
37
+ }
You can’t perform that action at this time.
0 commit comments