36. Construct Binary Tree from Inorder and Postorder Traversal && 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 assume that duplicates do not exist in the tree.

思想: 迭代。

说明: 这类问题,要求一个提供根节点,然后另一个序列(中序序列)可依据根节点分出左右子树。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
TreeNode *createTree(vector<int> &inorder, int start, int end, vector<int> &postorder, int start2, int end2) {
    if(start > end || start2 > end2) return NULL;
    TreeNode *root = new TreeNode(postorder[end2]);
    int i;
    for(i = start; i <= end; ++i)
        if(inorder[i] == postorder[end2]) break;
   // if(i > end) throws std::exception("error");
    root->left = createTree(inorder, start, i-1, postorder, start2, start2 + i-start-1);
    root->right = createTree(inorder, i+1, end, postorder, start2+i-start, end2-1);
    return root;
}

class Solution {
public:
    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
        return createTree(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1);
    }
};

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 binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
TreeNode* createTree(vector<int> &preorder, int start, int end, vector<int> &inorder, int start2, int end2) {
    if(start > end || start2 > end2) return NULL;
    TreeNode *root = new TreeNode(preorder[start]);
    int i;
    for(i = start2; i <= end2; ++i)
        if(preorder[start] == inorder[i]) break;
    root->left = createTree(preorder, start+1, start+i-start2, inorder, start2, i-1);
    root->right = createTree(preorder, start+i-start2+1, end, inorder, i+1, end2);
    return root;
}
class Solution {
public:
    TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
        return createTree(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1);
    }
};
时间: 2024-10-04 07:07:38

36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal的相关文章

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!! 下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || po

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

[106-Construct Binary Tree from Inorder and Postorder Traversal(通过中序和后序遍历构造二叉树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in th

Jan 28 - Construct Binary Tree From Preorder And Inorder; Tree; DFS; Array

Using DFS to traverse the node and build the a tree. for a node, it has following properties: If its a left child node of its parent, then the left boundary start of in the inorder array is its parent's location in inorder array. Let inorderPos be th

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

【leetcode】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. 1 class Solution { 2 public: 3 TreeNode* createTree(vector<int>& inorder,int instart,int inend,vector<in

【Leetcode】【Medium】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. 解题思路: 给出一个二叉树的中序和后序遍历结果,还原这个二叉树. 对于一个二叉树: 1 / 2 3 / \ / 4 5 6 7 后序遍历结果为:4 5 2 6 7 3 1 中序遍历结果为:4 2 5 1 6 3 7 由此可以

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

LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. (Medium) Note:You may assume that duplicates do not exist in the tree. 分析: 跟前一题方法一样,只需要分析出中序与后序恢复时,需从后序的最后一个元素读出来当做根节点,在中序中寻找其位置. 其余递归过程跟前序中序恢复一致. 代码: 1 /** 2 * Definition fo

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

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. Hide Tags Tree Array Depth-first Search SOLUTION 1: 使