题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
思路:根据中序遍历和前序遍历可以确定二叉树,具体过程为:
1、根据前序序列第一个结点确定根结点;
2、根据根结点在中序序列中的位置分割出左右两个子序列;
3、对左子树和右子树分别递归使用同样的方法继续分解
例如:
前序序列{1,2,4,7,3,5,6,8} = pre
中序序列{4,7,2,1,5,3,8,6} = in
1、根据当前前序序列的第一个结点确定根结点,为 1,找到 1 在中序遍历序列中的位置,为 in[3];
2、切割左右子树,则 in[3] 前面的为左子树, in[3] 后面的为右子树,则切割后的左子树前序序列为:{2,4,7},切割后的左子树中序序列为:{4,7,2};切割后的右子树前序序列为:{3,5,6,8},切割后的右子树中序序列为:{5,3,8,6};
3、对子树分别使用同样的方法分解
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# 返回构造的TreeNode根节点
def reConstructBinaryTree(self, pre, tin):
# write code here0
if len(pre)==0:
return None
if len(pre)==1:
return TreeNode(pre[0])
root=TreeNode(pre[0])
tinL=tin[:tin.index(pre[0])]
tinR=tin[tin.index(pre[0])+1:]
root.left=self.reConstructBinaryTree(pre[1:tin.index(pre[0])+1],tinL)
root.right=self.reConstructBinaryTree(pre[tin.index(pre[0])+1:],tinR)
return root
换一种更简洁的写法
class Treenode:
def __init__(self, val):
self.val = val
self.leftchild = None
self.rightchild = None
#知道前序和中序,重建二叉树
def rebuildtree(prorder, inorder):
if len(prorder) == 0:
return None
else:
root = Treenode(prorder[0])
k = inorder.index(prorder[0])
root.leftchild = rebuildtree(prorder[1:k + 1], inorder[:k])
root.rightchild = rebuildtree(prorder[k + 1:], inorder[k + 1:])
return root
本文介绍如何利用前序遍历和中序遍历结果重建二叉树的方法,通过递归算法,详细解析了从序列到树的转换过程。
580

被折叠的 条评论
为什么被折叠?



