Skip to content

Instantly share code, notes, and snippets.

@HoweChen
Last active March 14, 2019 08:50
Show Gist options
  • Save HoweChen/9fa623f4cdaf2259b8c96d1b4ab5f23d to your computer and use it in GitHub Desktop.
Save HoweChen/9fa623f4cdaf2259b8c96d1b4ab5f23d to your computer and use it in GitHub Desktop.
[inorderBST]using yield and yiled from #Python #Algorithm
class Solution:
def increasingBST(self, root):
def inorder(node):
if node:
# 这里用了yield 和 yield from
# yield from 作为递归
# yield 返回值
# 这是一个左中右的 inorder 遍历
# 返回一个 generator
yield from inorder(node.left)
yield node.val
yield from inorder(node.right)
ans = cur = TreeNode(None)
for v in inorder(root):
cur.right = TreeNode(v)
cur = cur.right
return ans.right
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment