-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertIntoBST.py
67 lines (52 loc) · 1.57 KB
/
insertIntoBST.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : insertIntoBST.py
@Time : 2024/10/16 10:57:06
@Author : Zihao Zheng
@Email : zhengzihao718@163.com
'''
class TreeNode():
def __init__(self,val=0,left=None,right=None):
self.value=val
self.left=left
self.right=right
class Solution():
def __init__(self) -> None:
self.parent=None
def insertIntoBST(self,root,val):
if root is None:
return TreeNode(val)
cur=root
parent=root
while cur:
parent=cur
if val<cur.left.value:
cur=cur.left
else:
cur=cur.right
if val<parent.value:
parent.left=TreeNode(val)
else:
parent.right=TreeNode(val)
return root
def traversal(self, cur, val):
if cur is None:
node = TreeNode(val)
if val > self.parent.value:
self.parent.right = node
else:
self.parent.left = node
return
self.parent = cur #保存父节点
if cur.value > val:
self.traversal(cur.left, val)
if cur.value < val:
self.traversal(cur.right, val)
#整个函数不需要返回值
def insertIntoBST2(self,root,val):
self.parent = TreeNode(0)
if root is None:
return TreeNode(val)
self.traversal(root, val)
return root #在这里返回