Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

面试题 6 重建二叉树 的一处写法可以改进 #1

Open
mookrs opened this issue Aug 20, 2018 · 1 comment
Open

面试题 6 重建二叉树 的一处写法可以改进 #1

mookrs opened this issue Aug 20, 2018 · 1 comment

Comments

@mookrs
Copy link

mookrs commented Aug 20, 2018

root.right = construct_tree(preorder[-len(right):], right)

这里如果 right[] 的话,会导致 preorder[-len(right):] 的值变成该 preorder 的副本,前序遍历和中序遍历长度不一致,显然是不合理的。

应该写成

root.right = construct_tree(preorder[index + 1:], right)

或者对这一段重新整理一下

index = inorder.index(preorder[0])
root = TreeNode(preorder[0])
root.left = construct_tree(preorder[1:index + 1], inorder[0:index])
root.right = construct_tree(preorder[index + 1:], inorder[index + 1:])
return root
@JushuangQiao
Copy link
Owner

感谢意见,我再看一下,优化一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants