提交 dfdb871e 编写于 作者: L laozhang

add python 701 and python 173

上级 37ec15a7
......@@ -47,6 +47,8 @@
33. [祖父节点值为偶数的节点和](./solution/tree/leetcode_1315_.py)
34. [两棵二叉搜索树中的所有元素](./solution/tree/leetcode_1305_.py)
35. [二叉树剪枝](./solution/tree/leetcode_814_.py)
36. [二叉搜索树中的插入操作](./solution/tree/leetcode_701_.py)
37. [二叉搜索树迭代器](./solution/tree/leetcode_173_.py)
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# coding=utf-8
"""
173. 二叉搜索树迭代器
"""
from solution import TreeNode
class BSTIterator:
list = []
def __init__(self, root: TreeNode):
while root:
self.list.append(root)
root = root.left
def next(self) -> int:
"""
@return the next smallest number
"""
node = self.list.pop()
t = node.right
while (t):
self.list.append(t)
t = t.left
return node.val
def hasNext(self) -> bool:
"""
@return whether we have a next smallest number
"""
return len(self.list) != 0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# coding=utf-8
"""
701. 二叉搜索树中的插入操作
"""
from solution import TreeNode
class Solution:
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
if not root:
return TreeNode(val)
if root.val >= val:
root.left = self.insertIntoBST(root.left, val)
else:
root.right = self.insertIntoBST(root.right, val)
return root
helper(root)
return res
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册