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

Commit 78d8729

Browse files
committed
solve problem Insert Into A Binary Search Tree
1 parent ccd6a41 commit 78d8729

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ All solutions will be accepted!
305305
|921|[Minimum Add To Make Parentheses Valid](https://leetcode-cn.com/problems/minimum-add-to-make-parentheses-valid/description/)|[java/py/js](./algorithms/MinimumAddToMakeParenthesesValid)|Medium|
306306
|797|[All Paths From Source To Target](https://leetcode-cn.com/problems/all-paths-from-source-to-target/description/)|[java/py/js](./algorithms/AllPathsFromSourceToTarget)|Medium|
307307
|789|[Escape The Ghosts](https://leetcode-cn.com/problems/escape-the-ghosts/description/)|[java/py/js](./algorithms/EscapeTheGhosts)|Medium|
308+
|701|[Insert Into A Binary Search Tree](https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/description/)|[java/py/js](./algorithms/InsertIntoABinarySearchTree)|Medium|
308309

309310
# Database
310311
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Insert Into A Binary Search Tree
2+
This problem is easy to solve
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode insertIntoBST(TreeNode root, int val) {
12+
if (root == null)
13+
return new TreeNode(val);
14+
15+
TreeNode p = root,
16+
pre = null;
17+
18+
while (p != null) {
19+
pre = p;
20+
if (val < p.val)
21+
p = p.left;
22+
else
23+
p = p.right;
24+
}
25+
26+
if (val < pre.val)
27+
pre.left = new TreeNode(val);
28+
else
29+
pre.right = new TreeNode(val);
30+
31+
return root;
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {number} val
11+
* @return {TreeNode}
12+
*/
13+
var insertIntoBST = function(root, val) {
14+
if (!root)
15+
return new TreeNode(val)
16+
17+
let p = root,
18+
pre = null
19+
20+
while (p) {
21+
pre = p
22+
if (val < p.val)
23+
p = p.left
24+
else
25+
p = p.right
26+
}
27+
28+
if (val < pre.val)
29+
pre.left = new TreeNode(val)
30+
else
31+
pre.right = new TreeNode(val)
32+
33+
return root
34+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def insertIntoBST(self, root, val):
10+
"""
11+
:type root: TreeNode
12+
:type val: int
13+
:rtype: TreeNode
14+
"""
15+
if not root:
16+
return TreeNode(val)
17+
18+
p = root
19+
pre = root
20+
while p:
21+
pre = p
22+
if val < p.val:
23+
p = p.left
24+
else:
25+
p = p.right
26+
27+
if val < pre.val:
28+
pre.left = TreeNode(val)
29+
else:
30+
pre.right = TreeNode(val)
31+
32+
return root

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /