106. Construct Binary Tree from Inorder and Postorder Traversal

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

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

========

利用:中序+后序遍历

====

code:

/**
 * 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:
    ///
    template<typename Iterator>
    TreeNode *help_buildTree_ip(Iterator in_first,Iterator in_last,
        Iterator post_first,Iterator post_last){
        if(in_first == in_last) return nullptr;
        if(post_first == post_last) return nullptr;

        auto val = *prev(post_last);
        TreeNode *root = new TreeNode(val);

        auto in_root_pos = find(in_first,in_last,val);
        auto left_size = distance(in_first,in_root_pos);
        auto post_left_last = next(post_first,left_size);

        root->left = help_buildTree_ip(in_first,in_root_pos,post_first,post_left_last);
        root->right = help_buildTree_ip(next(in_root_pos),in_last,post_left_last,prev(post_last));

        return root;
    }
    TreeNode* buildTree_ip(vector<int> &inorder,vector<int> &postorder){
        return help_buildTree_ip(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
    }
};
时间: 2024-08-10 19:02:49

106. Construct Binary Tree from Inorder and Postorder Traversal的相关文章

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not

Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: 115870 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 解题思路

C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 这个题目是给你一棵树的中序遍历和后序遍历,让你将这棵树表示出来.其中可以假设在树中没有重复的元素. 当做完这个题之后,建议去做做第105题,跟这道题类似. 分析:这个解法的基本思想是:我们有两个数组,分别是IN和POST.后

leetCode 106.Construct Binary Tree from Inorder and Postorder Traversal (根据中序遍历和后序遍历构造二叉树)

Given inorder and postorder 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. * public class TreeNod

106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 题目标签:Array, Tree 这到题目和105 几乎是一摸一样的,唯一的区别就是把pre-order 换成 post-order.因为post-order是最后一个数字是root,所以要从右向左的遍历.还需要把helpe

【LeetCode】105 &amp; 106. Construct Binary Tree from Inorder and Postorder Traversal

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 提示: 题目要求通过一颗二叉树的中序遍历及后续遍历的结果,将这颗二叉树构建出来,另外还有一个已知条件,所有节点的值都是不同的. 首先需要了解一下二叉树不同遍历方式的定义: 前序遍历:首先访问根结点,然后遍历左子树,最

106. Construct Binary Tree from Inorder and Postorder Traversal根据后中序数组恢复出原来的树

[抄题]: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree: 3 /

106. Construct Binary Tree from Inorder and Postorder Traversal - Medium

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree: 3 / 9 20