LeetCode 105,106. Construct Binary Tree 重建二叉树 Python Solution
来源:互联网 发布:怎样添加图表数据 编辑:程序博客网 时间:2024/06/05 01:18
此题目对应于105. Construct Binary Tree from Preorder and Inorder Traversal 和 106. Construct Binary Tree from Inorder and Postorder Traversal
题目要求:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
这里必须给出两个遍历才可以重建二叉树,其中前序或者后续的遍历能给出树根节点的值,
中序遍历起到划分二叉树左右子树的作业。
距离说明:输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建出如下图所示的二叉树并输出它的头结点。
解题思路:
根据先序向量数组的值把中序向量数组一分为二,然后递归左右部分;
在二叉树的前序遍历和中序遍历的序列中确定根结点的值、左子树结点的值和右子树结点的值的步骤如下图所示:
下面给出Python代码:
# -*- coding:utf-8 -*-class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None'''输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。'''class Solution(object): # 根据前序和中序遍历重建二叉树 def buildTree(self, preorder, inorder): if len(preorder)==0: return None if len(preorder)==1: return TreeNode(preorder[0]) root_val = preorder[0] idx = inorder.index(root_val) root = TreeNode(root_val) root.left = self.buildTree(preorder[1:idx+1],inorder[:idx]) root.right = self.buildTree(preorder[idx+1:],inorder[idx+1:]) return root # 根据后序和中序遍历重建二叉树 def buildTree(self, inorder, postorder): if len(inorder)==0: return None if len(inorder)==1: return TreeNode(inorder[0]) root_v = postorder[-1] root = TreeNode(root_v) idx = inorder.index(root_v) root.left = self.buildTree(inorder[:idx],postorder[:idx]) root.right = self.buildTree(inorder[idx+1:],postorder[idx:-1]) return root """ :type inorder: List[int] :type postorder: List[int] :rtype: TreeNode """
参考文献:
http://www.cnblogs.com/edisonchou/p/4741099.html
阅读全文
0 0
- LeetCode 105,106. Construct Binary Tree 重建二叉树 Python Solution
- LeetCode-105、106 Construct Binary Tree (JAVA)重建二叉树
- 105\106. Construct Binary Tree from Inorder and Postorder\Preorder Traversal&重建二叉树
- 重建二叉树106. Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal 从先序和中序重建二叉树@LeetCode
- Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序重建二叉树@LeetCode
- LeetCode 144,94,145,102,103 Binary Tree Traversal 二叉树遍历 Python Solution
- LeetCode 114. Flatten Binary Tree to Linked List 拉平二叉树 Python Solution
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal, Solution
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- Construct Binary Tree from Preorder and Inorder Traversal 前序和中序重建二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序重建二叉树
- 重建二叉树105. Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 105 /106 . Construct Binary Tree
- 重建二叉树 Binary Tree Traversals HDU
- 根据前序和中序序列重建二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- 根据中序和后序序列重建二叉树 Construct Binary Tree from Inorder and Postorder Traversal
- 剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告
- 解决CentOS5.5无法使用ifconfig命令
- Herosphp 3.0.1 发布,轻量级php开源框架
- APIGateway 简介
- VS2010调试的时候怎么看函数返回值
- UVALive 7501Business Cycle(二分,xjb乱判)
- LeetCode 105,106. Construct Binary Tree 重建二叉树 Python Solution
- MySQL数据库
- JAVA运算符
- 资料|云计算之PaaS经典案例-上篇+中篇
- 二叉树非递归层次遍历
- ecshop文件结构详解
- mac搭建本地svn服务
- Ubuntu 11.10下解决Gedit中文乱码的问题
- jeesite内存溢出与单用户登陆