|
11 | 11 |
|
12 | 12 | > 验证二叉搜索树
|
13 | 13 |
|
14 |
| -```go |
15 |
| -/** |
16 |
| - * Definition for a binary tree node. |
17 |
| - * type TreeNode struct { |
18 |
| - * Val int |
19 |
| - * Left *TreeNode |
20 |
| - * Right *TreeNode |
21 |
| - * } |
22 |
| - */ |
23 |
| -func isValidBST(root *TreeNode) bool { |
24 |
| - return dfs(root).valid |
25 |
| -} |
26 |
| -type ResultType struct{ |
27 |
| - max int |
28 |
| - min int |
29 |
| - valid bool |
30 |
| -} |
31 |
| -func dfs(root *TreeNode)(result ResultType){ |
32 |
| - if root==nil{ |
33 |
| - result.max=-1<<63 |
34 |
| - result.min=1<<63-1 |
35 |
| - result.valid=true |
36 |
| - return |
37 |
| - } |
38 |
| - |
39 |
| - left:=dfs(root.Left) |
40 |
| - right:=dfs(root.Right) |
41 |
| - |
42 |
| - // 1、满足左边最大值<root<右边最小值 && 左右两边valid |
43 |
| - if root.Val>left.max && root.Val<right.min && left.valid && right.valid { |
44 |
| - result.valid=true |
45 |
| - } |
46 |
| - // 2、更新当前节点的最大最小值 |
47 |
| - result.max=Max(Max(left.max,right.max),root.Val) |
48 |
| - result.min=Min(Min(left.min,right.min),root.Val) |
49 |
| - return |
50 |
| -} |
51 |
| -func Max(a,b int)int{ |
52 |
| - if a>b{ |
53 |
| - return a |
54 |
| - } |
55 |
| - return b |
56 |
| -} |
57 |
| -func Min(a,b int)int{ |
58 |
| - if a>b{ |
59 |
| - return b |
60 |
| - } |
61 |
| - return a |
62 |
| -} |
63 |
| - |
| 14 | +```Python |
| 15 | +class Solution: |
| 16 | + def isValidBST(self, root: TreeNode) -> bool: |
| 17 | + |
| 18 | + if root is None: |
| 19 | + return True |
| 20 | + |
| 21 | + s = [(root, float('-inf'), float('inf'))] |
| 22 | + while len(s) > 0: |
| 23 | + node, low, up = s.pop() |
| 24 | + if node.left is not None: |
| 25 | + if node.left.val <= low or node.left.val >= node.val: |
| 26 | + return False |
| 27 | + s.append((node.left, low, node.val)) |
| 28 | + if node.right is not None: |
| 29 | + if node.right.val <= node.val or node.right.val >= up: |
| 30 | + return False |
| 31 | + s.append((node.right, node.val, up)) |
| 32 | + return True |
64 | 33 | ```
|
65 | 34 |
|
66 | 35 | [insert-into-a-binary-search-tree](https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/)
|
67 | 36 |
|
68 | 37 | > 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 保证原始二叉搜索树中不存在新值。
|
69 | 38 |
|
70 |
| -```go |
71 |
| -func insertIntoBST(root *TreeNode, val int) *TreeNode { |
72 |
| - if root==nil{ |
73 |
| - return &TreeNode{Val:val} |
74 |
| - } |
75 |
| - if root.Val<val{ |
76 |
| - root.Right=insertIntoBST(root.Right,val) |
77 |
| - }else{ |
78 |
| - root.Left=insertIntoBST(root.Left,val) |
79 |
| - } |
80 |
| - return root |
81 |
| -} |
| 39 | +```Python |
| 40 | +class Solution: |
| 41 | + def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: |
| 42 | + |
| 43 | + if root is None: |
| 44 | + return TreeNode(val) |
| 45 | + |
| 46 | + if val > root.val: |
| 47 | + root.right = self.insertIntoBST(root.right, val) |
| 48 | + else: |
| 49 | + root.left = self.insertIntoBST(root.left, val) |
| 50 | + |
| 51 | + return root |
82 | 52 | ```
|
83 | 53 |
|
84 | 54 | [delete-node-in-a-bst](https://leetcode-cn.com/problems/delete-node-in-a-bst/)
|
85 | 55 |
|
86 | 56 | > 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
|
87 | 57 |
|
88 |
| -```go |
89 |
| -/** |
90 |
| - * Definition for a binary tree node. |
91 |
| - * type TreeNode struct { |
92 |
| - * Val int |
93 |
| - * Left *TreeNode |
94 |
| - * Right *TreeNode |
95 |
| - * } |
96 |
| - */ |
97 |
| -func deleteNode(root *TreeNode, key int) *TreeNode { |
98 |
| - // 删除节点分为三种情况: |
99 |
| - // 1、只有左节点 替换为右 |
100 |
| - // 2、只有右节点 替换为左 |
101 |
| - // 3、有左右子节点 左子节点连接到右边最左节点即可 |
102 |
| - if root ==nil{ |
103 |
| - return root |
104 |
| - } |
105 |
| - if root.Val<key{ |
106 |
| - root.Right=deleteNode(root.Right,key) |
107 |
| - }else if root.Val>key{ |
108 |
| - root.Left=deleteNode(root.Left,key) |
109 |
| - }else if root.Val==key{ |
110 |
| - if root.Left==nil{ |
111 |
| - return root.Right |
112 |
| - }else if root.Right==nil{ |
113 |
| - return root.Left |
114 |
| - }else{ |
115 |
| - cur:=root.Right |
116 |
| - // 一直向左找到最后一个左节点即可 |
117 |
| - for cur.Left!=nil{ |
118 |
| - cur=cur.Left |
119 |
| - } |
120 |
| - cur.Left=root.Left |
121 |
| - return root.Right |
122 |
| - } |
123 |
| - } |
124 |
| - return root |
125 |
| -} |
| 58 | +```Python |
| 59 | +class Solution: |
| 60 | + def deleteNode(self, root: TreeNode, key: int) -> TreeNode: |
| 61 | + |
| 62 | + # try to find the node |
| 63 | + dummy = TreeNode(left=root) |
| 64 | + parent, node = dummy, root |
| 65 | + isleft = True |
| 66 | + while node is not None and node.val != key: |
| 67 | + parent = node |
| 68 | + isleft = key < node.val |
| 69 | + node = node.left if isleft else node.right |
| 70 | + |
| 71 | + # if found |
| 72 | + if node is not None: |
| 73 | + if node.right is None: |
| 74 | + if isleft: |
| 75 | + parent.left = node.left |
| 76 | + else: |
| 77 | + parent.right = node.left |
| 78 | + elif node.left is None: |
| 79 | + if isleft: |
| 80 | + parent.left = node.right |
| 81 | + else: |
| 82 | + parent.right = node.right |
| 83 | + else: |
| 84 | + p, n = node, node.left |
| 85 | + while n.right is not None: |
| 86 | + p, n = n, n.right |
| 87 | + if p != node: |
| 88 | + p.right = n.left |
| 89 | + else: |
| 90 | + p.left = n.left |
| 91 | + n.left, n.right = node.left, node.right |
| 92 | + if isleft: |
| 93 | + parent.left = n |
| 94 | + else: |
| 95 | + parent.right = n |
| 96 | + |
| 97 | + return dummy.left |
126 | 98 | ```
|
127 | 99 |
|
128 | 100 | [balanced-binary-tree](https://leetcode-cn.com/problems/balanced-binary-tree/)
|
129 | 101 |
|
130 | 102 | > 给定一个二叉树,判断它是否是高度平衡的二叉树。
|
131 | 103 |
|
132 |
| -```go |
133 |
| -type ResultType struct{ |
134 |
| - height int |
135 |
| - valid bool |
136 |
| -} |
137 |
| -func isBalanced(root *TreeNode) bool { |
138 |
| - return dfs(root).valid |
139 |
| -} |
140 |
| -func dfs(root *TreeNode)(result ResultType){ |
141 |
| - if root==nil{ |
142 |
| - result.valid=true |
143 |
| - result.height=0 |
144 |
| - return |
145 |
| - } |
146 |
| - left:=dfs(root.Left) |
147 |
| - right:=dfs(root.Right) |
148 |
| - // 满足所有特点:二叉搜索树&&平衡 |
149 |
| - if left.valid&&right.valid&&abs(left.height,right.height)<=1{ |
150 |
| - result.valid=true |
151 |
| - } |
152 |
| - result.height=Max(left.height,right.height)+1 |
153 |
| - return |
154 |
| -} |
155 |
| -func abs(a,b int)int{ |
156 |
| - if a>b{ |
157 |
| - return a-b |
158 |
| - } |
159 |
| - return b-a |
160 |
| -} |
161 |
| -func Max(a,b int)int{ |
162 |
| - if a>b{ |
163 |
| - return a |
164 |
| - } |
165 |
| - return b |
166 |
| -} |
167 |
| - |
| 104 | +```Python |
| 105 | +class Solution: |
| 106 | + def isBalanced(self, root: TreeNode) -> bool: |
| 107 | + |
| 108 | + # post-order iterative |
| 109 | + |
| 110 | + s = [[TreeNode(), -1, -1]] |
| 111 | + node, last = root, None |
| 112 | + while len(s) > 1 or node is not None: |
| 113 | + if node is not None: |
| 114 | + s.append([node, -1, -1]) |
| 115 | + node = node.left |
| 116 | + if node is None: |
| 117 | + s[-1][1] = 0 |
| 118 | + else: |
| 119 | + peek = s[-1][0] |
| 120 | + if peek.right is not None and last != peek.right: |
| 121 | + node = peek.right |
| 122 | + else: |
| 123 | + if peek.right is None: |
| 124 | + s[-1][2] = 0 |
| 125 | + last, dl, dr = s.pop() |
| 126 | + if abs(dl - dr) > 1: |
| 127 | + return False |
| 128 | + d = max(dl, dr) + 1 |
| 129 | + if s[-1][1] == -1: |
| 130 | + s[-1][1] = d |
| 131 | + else: |
| 132 | + s[-1][2] = d |
| 133 | + |
| 134 | + return True |
168 | 135 | ```
|
169 | 136 |
|
170 | 137 | ## 练习
|
|
0 commit comments