【Construct Binary Tree from Preorder and Inorder Traversal】cpp

题目:

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 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) {
        if ( preorder.size()==0 || inorder.size()==0 ) return NULL;
        return Solution::buildTreePI(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1);
    }
        static TreeNode* buildTreePI(
            vector<int>& preorder,
            int beginP, int endP,
            vector<int>& inorder,
            int beginI, int endI)
        {
            // terminal condition & corner case
            if ( beginP>endP ) return NULL;
            // resurisve process
            TreeNode *root = new TreeNode(-1);
            root->val = preorder[beginP];
            // find the root node in inorder traversal
            int rootPosInorder = beginI;
            for ( int i = beginI; i <= endI; ++i )
            {
                if ( inorder[i]==root->val ) { rootPosInorder=i; break;}
            }
            int leftSize = rootPosInorder - beginI;
            int rightSize = endI - rootPosInorder;
            root->left = Solution::buildTreePI(preorder, beginP+1, beginP+leftSize, inorder, beginI, rootPosInorder-1);
            root->right = Solution::buildTreePI(preorder, endP-rightSize+1, endP, inorder, rootPosInorder+1, endI);
            return root;
        }
};

tips:

经典题目。直接学习高手代码

http://fisherlei.blogspot.sg/2013/01/leetcode-construct-binary-tree-from.html

http://bangbingsyb.blogspot.sg/2014/11/leetcode-construct-binary-tree-from.html

这里注意在递归传递vector元素下标的时候,一定是绝对下标(一开始疏忽写成了相对下标,debug了不少时间)

时间: 2024-10-10 02:35:01

【Construct Binary Tree from Preorder and Inorder Traversal】cpp的相关文章

【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

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

【LeetCode-面试算法经典-Java实现】【105-Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)】

[105-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

[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 @ 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