Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfa47c47

Browse files
committed
Added a C# solution to 701. Insert into a Binary Search Tree
1 parent2ae20b0 commitfa47c47

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
8+
* this.val = val;
9+
* this.left = left;
10+
* this.right = right;
11+
* }
12+
* }
13+
*/
14+
publicclassSolution
15+
{
16+
publicTreeNodeInsertIntoBST(TreeNoderoot,intval)
17+
{
18+
if(rootisnull)returnnewTreeNode(val);
19+
20+
TreeNodecur=root;
21+
22+
while(curis notnull)
23+
{
24+
if(cur.val<val)
25+
{
26+
if(cur.rightisnull)
27+
{
28+
cur.right=newTreeNode(val);
29+
break;
30+
}
31+
32+
cur=cur.right;
33+
continue;
34+
}
35+
36+
if(cur.val>val)
37+
{
38+
if(cur.leftisnull)
39+
{
40+
cur.left=newTreeNode(val);
41+
break;
42+
}
43+
44+
cur=cur.left;
45+
continue;
46+
}
47+
}
48+
49+
returnroot;
50+
}
51+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp