-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0701-Insert-into-a-binary-search-tree.cs
More file actions
43 lines (37 loc) · 1.01 KB
/
0701-Insert-into-a-binary-search-tree.cs
File metadata and controls
43 lines (37 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0701.Insert_into_a_binary_search_tree
{
public class _0701_Insert_into_a_binary_search_tree
{
public TreeNode InsertIntoBST(TreeNode root, int val)
{
if (root == null) return root = new TreeNode(val);
TreeNode curr = root;
while (curr != null)
{
if (curr.val > val)
{
if (curr.left == null)
{
curr.left = new TreeNode(val);
break;
}
curr = curr.left;
}
else
{
if (curr.right == null)
{
curr.right = new TreeNode(val);
break;
}
curr = curr.right;
}
}
return root;
}
}
}