mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1200 from Camille0512/master
Add one more python code
This commit is contained in:
@ -408,7 +408,28 @@ class Solution:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
```
|
```
|
||||||
## Go
|
```python
|
||||||
|
# 遵循Carl的写法,只添加了节点判断的部分
|
||||||
|
class Solution:
|
||||||
|
def isValidBST(self, root: TreeNode) -> bool:
|
||||||
|
# method 2
|
||||||
|
que, pre = [], None
|
||||||
|
while root or que:
|
||||||
|
while root:
|
||||||
|
que.append(root)
|
||||||
|
root = root.left
|
||||||
|
root = que.pop()
|
||||||
|
# 对第一个节点只做记录,对后面的节点进行比较
|
||||||
|
if pre is None:
|
||||||
|
pre = root.val
|
||||||
|
else:
|
||||||
|
if pre >= root.val: return False
|
||||||
|
pre = root.val
|
||||||
|
root = root.right
|
||||||
|
return True
|
||||||
|
```
|
||||||
|
|
||||||
|
## Go
|
||||||
|
|
||||||
```Go
|
```Go
|
||||||
import "math"
|
import "math"
|
||||||
|
@ -437,41 +437,6 @@ class Solution:
|
|||||||
return True
|
return True
|
||||||
```
|
```
|
||||||
|
|
||||||
层序遍历
|
|
||||||
|
|
||||||
```python
|
|
||||||
class Solution:
|
|
||||||
def isSymmetric(self, root: TreeNode) -> bool:
|
|
||||||
if not root: return True
|
|
||||||
que, cnt = [[root.left, root.right]], 1
|
|
||||||
while que:
|
|
||||||
nodes, tmp, sign = que.pop(), [], False
|
|
||||||
for node in nodes:
|
|
||||||
if not node:
|
|
||||||
tmp.append(None)
|
|
||||||
tmp.append(None)
|
|
||||||
else:
|
|
||||||
if node.left:
|
|
||||||
tmp.append(node.left)
|
|
||||||
sign = True
|
|
||||||
else:
|
|
||||||
tmp.append(None)
|
|
||||||
if node.right:
|
|
||||||
tmp.append(node.right)
|
|
||||||
sign = True
|
|
||||||
else:
|
|
||||||
tmp.append(None)
|
|
||||||
p1, p2 = 0, len(nodes) - 1
|
|
||||||
while p1 < p2:
|
|
||||||
if (not nodes[p1] and nodes[p2]) or (nodes[p1] and not nodes[p2]): return False
|
|
||||||
elif nodes[p1] and nodes[p2] and nodes[p1].val != nodes[p2].val: return False
|
|
||||||
p1 += 1
|
|
||||||
p2 -= 1
|
|
||||||
if sign: que.append(tmp)
|
|
||||||
cnt += 1
|
|
||||||
return True
|
|
||||||
```
|
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user