Skip to content

Commit

Permalink
添加 0701.二叉搜索树中的插入操作 go版(迭代法)
Browse files Browse the repository at this point in the history
  • Loading branch information
NevS authored Jul 1, 2021
1 parent 0f38eb9 commit d5e128e
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions problems/0701.二叉搜索树中的插入操作.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ class Solution:


Go:

递归法

```Go
func insertIntoBST(root *TreeNode, val int) *TreeNode {
if root == nil {
Expand All @@ -285,6 +288,31 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode {
return root
}
```
迭代法
```go
func insertIntoBST(root *TreeNode, val int) *TreeNode {
if root == nil {
return &TreeNode{Val:val}
}
node := root
var pnode *TreeNode
for node != nil {
if val > node.Val {
pnode = node
node = node.Right
} else {
pnode = node
node = node.Left
}
}
if val > pnode.Val {
pnode.Right = &TreeNode{Val: val}
} else {
pnode.Left = &TreeNode{Val: val}
}
return root
}
```

JavaScript版本

Expand Down

0 comments on commit d5e128e

Please sign in to comment.