已知前序、中序构造二叉树(关键词:二叉树/前序/先序/中序/后序/先根/中根/后根/遍历/搜索/查找)

版权声明:本文为博主原创文章,可以转载,但转载前请联系博主。 https://blog.csdn.net/qq_33528613/article/details/84954506

已知前序、中序构造二叉树

实现

def buildTree(self, preorder, inorder):
	if inorder:
		rootVal = preorder.pop(0)
		rootIdx = inorder.index(rootVal)

		root = TreeNode(rootVal)
		root.left = self.buildTree(preorder, inorder[:rootIdx])
		root.right = self.buildTree(preorder, inorder[rootIdx+1:])

	return root

参考文献

  1. 105. Construct Binary Tree from Preorder and Inorder Traversal - LeetCode
  2. 这是印象笔记中的笔记,如果是在CSDN手机APP上查看此博客,请在印象笔记手机APP中搜索该参考文献:https://app.yinxiang.com/shard/s44/nl/9329661/5afbcd1d-6289-410d-9580-c54e9c412c97;
  3. 20 前序中序求后序

猜你喜欢

转载自blog.csdn.net/qq_33528613/article/details/84954506