Construct Binary Tree From Inorder and Preorder/Postorder Traversal

map<int, int> mapIndex;
void mapToIndex(int inorder[], int n)
{
    for (int i = 0; i < n; i++)
    {
        mapIndex.insert(map<int, int>::value_type(inorder[i], i);
    }
}

Node* buildInorderPreorder(int in[], int pre[], int n, int offset)
{
    assert(n >= 0);
    if (n == 0)
        return NULL;
    int rootVal = pre[0];
    int i = mapIndex[rootVal] - offset;
    Node* root = new Node(rootVal);
    root->left = buildInorderPreorder(in, pre+1, i, offset);
    root->right = buildInorderPreorder(in+i+1, pre+i+1, n-i-1, offset+i+1);
    return root;
}

Node* buildInorderPostorder(int in[], int post[], int n, int offset)
{
    assert(n >= 0);
    if (n == 0)
        return NULL;
    int rootVal = post[n-1];
    int i = mapIndex[rootVal] - offset;
    Node* root = new Node(rootVal);
    root->left = buildInorderPostorder(in, post, i, offset);
    root->right = buildINorderPostorder(in+i+1, post+i, n-i-1, offset+i+1);
    return root;
}
时间: 2024-12-19 19:55:17

Construct Binary Tree From Inorder and Preorder/Postorder Traversal的相关文章

Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) 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. 递归构建. 思路就是: preorder可以定位到根结点,inorder可以定位左右子树的取值范围. 1. 由

105. Construct Binary Tree from Inorder and preorder Traversal

/* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root一定在中间那个点,还是要找一下中间点的位置 * p.left = construct(preorder, preStart + 1, preStart + (k - inStart),inorder, inStart, k - 1); * p.right = construct(preorder,

Leetcode, construct binary tree from inorder and post order traversal

Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs about this problems: 1. http://siddontang.gitbooks.io/leetcode-solution/content/tree/construct_binary_tree.html 2.http://blog.csdn.net/linhuanmars/artic

LeetCode – Refresh – Construct Binary Tree from Inorder and Preorder Traversal

Only different with preorder and postorder is that the root start from the beginning for preorder. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NUL

Construct Binary Tree from Inorder and PreOrder Traversal

preOrder: (root) LLL RRRR inOrder: LLL (root) RRRR inOrder:  LLL RRRR (root) 根据preOrder/postOrder 可以知道root(一头一尾); 然后找到root在inorder中对应的位置(index) index左边即为Left Branch(inorder), 右边是Right Branch(inorder). 确定LeftBranch && RightBranch的长度后,同样可以从PreOrder/

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

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

Construct Binary Tree from Inorder and Postorder Traversal (算法课上的题)

Construct Binary Tree from Inorder and Postorder Traversal 这道题之前算法课上好像遇到过,思路也很简单的. 思路:后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可.(在去除了根节点之后,中序遍历和后序遍历的前N个树都是

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp;amp; Construct Binary Tree f

1.  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. 代码: class Solution { public: TreeNode *buildTr