【LeetCode每天一题】Construct Binary Tree from Preorder and Inorder 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.

For example, given

preorder = [3,9,20,15,7]
inorder   = [9,3,15,20,7]

Return the following binary tree:

    3
   /   9  20
    /     15   7

思路

  利用二叉树的前序遍历顺序和中序遍历顺序来构建二叉树,这道题我们可以利用前序遍历和中序遍历的特点来解决,前序遍历时第一个节点为根节点,然后我们使用该节点找出中序遍历中存在的位置下标,以中序遍历的特点我们可以知道,根节点的左子树一共存在几个节点,右子树一共存在几个节点。然后将前序遍历和中序遍历分成两部分,左子树节点和右子树节点。继续使用之前的方法来进行查找(前序遍历的第一个节点,找到中序遍历中存在的位置)。  另外,还有一道根据中序遍历和后序遍历结果来构建二叉树,这个思路和这道题的思路一模一样,只需要注意左子树和右子树的取值规则不一样而已。图示步骤


解决代码



 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def buildTree(self, preorder, inorder):
10         """
11         :type preorder: List[int]
12         :type inorder: List[int]
13         :rtype: TreeNode
14         """
15         if not preorder and not inorder:  # 为空时直接返回None
16             return None
17         if len(preorder) == 1 and len(inorder) == 1:   # 只有一个元素时,返回该元素的节点值
18             return TreeNode(preorder[0])
19         first = preorder[0]                # 前序遍历中第一个元素
20         index = inorder.index(first)        # 找出中序遍历中first元素的下标
21         root = TreeNode(first)              # 构建根节点
22         root.left = self.buildTree(preorder[1:index+1], inorder[:index])  # 求出左节点
23         root.right = self.buildTree(preorder[1+index:], inorder[index+1:])  # 求右节点
24         return root
25         


原文地址:https://www.cnblogs.com/GoodRnne/p/10858672.html

时间: 2024-10-08 12:11:00

【LeetCode每天一题】Construct Binary Tree from Preorder and Inorder Traversal(使用前序和中序遍历构建二叉树)的相关文章

leetcode题解:Construct Binary Tree from Preorder and Inorder 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)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树

给定一棵树的前序遍历与中序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出前序遍历 = [3,9,20,15,7]中序遍历 = [9,3,15,20,7]返回如下的二叉树:    3   / \  9  20    /  \   15   7详见:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/ /** * Definiti

leetcode_105题——Construct Binary Tree from Preorder and Inorder Traversal(树,递归)

Construct Binary Tree from Preorder and Inorder Traversal Total Accepted: 32279 Total Submissions: 122094My Submissions Question Solution Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates d

leetCode 105.Construct Binary Tree from Preorder and Inorder 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. 思路:首先根据前序遍历得到根节点,然后在中序遍历中得到根节点的位置,左边的为左子树,右边的为右子树. 然后再递归求解左子树和右子树的构造即可.代码如下: /** * Definition for a binary tree

【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder 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. 类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html 只是子树的前序和中序遍历序列分别更新为: //左子树: left_prestart = prestart+1 lef

[leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根据二叉树的先序遍历和中序遍历恢复二叉树. 解题思路:可以参照 http://www.cnblogs.com/zuoyuan/p/3720138.html 的思路.递归进行解决. 代码: # Definition for a binary tree node # class TreeNode: # d

[LeetCode]*105.Construct Binary Tree from Preorder and Inorder 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.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

Construct Binary Tree from Preorder and Inorder Traversal leetcode java

题目: 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 3 / \ / \ 4 5 6 7 对于上图的树来说, index: 0 1 2 3 4 5 6 先序遍历为: 1 2 4 5 3 6 7 中序遍历为: 4 2 5 1 6 3 7为了清晰表示

【leetcode】Construct Binary Tree from Preorder and Inorder Traversal

问题: 给定二叉树的前序和中序遍历,重构这课二叉树. 分析: 前序.中序.后序都是针对于根结点而言,所以又叫作先根.中根.后根(当然不是高跟). 前序:根  左 右 中序:左  根 右 对二叉树,我们将其进行投影,就会发现个有趣的事: 发现投影后的顺序,恰好是中序遍历的顺序,这也就是为什么在构造二叉树的时候,一定需要知道中序遍历,因为中序遍历决定了结点间的相对左右位置关系.所以,对一串有序的数组,我们可以来构建二叉有序数,并通过中序遍历,就可以得到这个有序的数组. 既然中序遍历可以通过根结点将序