Construct Binary Tree from Preorder and Inorder Traversal -- leetcode

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:

You may assume that duplicates do not exist in the tree.

基本思路:

从前序和中序遍历结果,构造出一个二叉树。

前序遍历为:   根 {左子树的所有结点} {右子树所有结点}

中序遍历为:   {左子树的所有结点} 根 {右子树所有结点}

即 前序的第一个元素为根, 用此元素,在中序遍历的中顺序找到其出现的位置。则此位置,分隔出了左子树和右子树。

而此位置, 同时也是 前序遍历中 左子树 与 右子树的 分界点。

两个序列的左子树和右子树的范围都确定后,可以重复上面的方法,递归应用到其左子树和右子树。

在leetcode上实际执行时间为53ms。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        return helper(preorder, inorder, 0, preorder.size(), 0, inorder.size());
    }

    TreeNode *helper(vector<int> &preorder, vector<int> &inorder, int pre_start, int pre_stop, int in_start, int in_stop) {
        if (pre_start >= pre_stop)
            return 0;

        while (inorder[in_start] != preorder[pre_start])
            in_start++;

        TreeNode *root = new TreeNode(preorder[pre_start]);
        root->left = helper(preorder, inorder, pre_start+1, pre_stop-in_stop+in_start+1, in_stop-pre_stop+pre_start, in_start);
        root->right = helper(preorder, inorder, pre_stop-in_stop+in_start+1, pre_stop, in_start+1, in_stop);
        return root;
    }
};
时间: 2024-11-07 23:00:12

Construct Binary Tree from Preorder and Inorder Traversal -- leetcode的相关文章

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为了清晰表示

Construct Binary Tree from Preorder and Inorder Traversal&lt;leetcode&gt;

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 * Definition for binary tree 3 * struct TreeNode { 4 * int val;

[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.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

43: Construct Binary Tree from Preorder and Inorder Traversal

/************************************************************************/            /*       43:  Construct Binary Tree from Preorder and Inorder Traversal                            */            /**************************************************

LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

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. SOLUTION 1: 1. Find the root node from the preorder.(it

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. 分析:通过一个二叉树的先序遍历和中序遍历可以确定一个二叉树.先序遍历的第一个元素对应二叉树的根结点,由于在中序遍历中左子树和右子树的中序遍历序列分别在根节点两侧,因此我们可以确定左子树和右子树的中序遍历序列.在先序遍历序列中,

【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

36. Construct Binary Tree from Inorder and Postorder Traversal &amp;&amp; Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assu