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.
struct Node
{
int val;
Node(int v)
{
val = v;
left = right = NULL;
}
Node *left;
Node *right;
};
Node* visit(vector<int> &preorder, int preLeft, int preRight, vector<int> &inorder, int inLeft, int inRight)
{
if (preLeft > preRight)
{
return NULL;
}
int index = inLeft;
for (;index <= inRight; index++)
{
if (inorder[index] == preorder[preLeft])
{
break;
}
}
int leftLength = index - inLeft;
Node *left = visit(preorder, preLeft+1, preLeft+leftLength, inorder, inLeft, index-1);
Node *right = visit(preorder, preLeft+leftLength+1, preRight, inorder, index+1, inRight);
Node *p = new Node(preorder[preLeft]);
p->left = left;
p->right = right;
return p;
}
Node* buildTree(vector<int> &preorder, vector<int> &inorder)
{
if (preorder.size() < 1)
{
return NULL;
}
return visit(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1);
}
相關文章
- Leetcode Construct Binary Tree from Preorder and Inorder TraversalLeetCodeStruct
- Construct Binary Tree from Preorder and Inorder Traversal leetcode javaStructLeetCodeJava
- Leetcode 105 Construct Binary Tree from Preorder and Inorder TraversalLeetCodeStruct
- Construct Binary Tree from Inorder and Postorder TraversalStruct
- Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode javaStructLeetCodeJava
- Leetcode Construct Binary Tree from Inorder and Postorder TraversalLeetCodeStruct
- Leetcode-Construct Binary Tree from inorder and preorder travesalLeetCodeStruct
- Leetcode 106. Construct Binary Tree from Inorder and Postorder TraversalLeetCodeStruct
- Construct Binary Tree from Preorder and Inorder Traversal(前序遍歷和中序遍歷樹構造二叉樹)...Struct二叉樹
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal 根據先序序列和中序序列恢復二叉樹LeetCodeStruct二叉樹
- Leetcode-Construct Binary Tree from inorder and postorder travesalLeetCodeStruct
- leetcode - Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Inorder TraversalLeetCode
- Binary Tree Preorder Traversal leetcode javaLeetCodeJava
- [leetcode]binary-tree-inorder-traversalLeetCode
- Leetcode-Binary Tree Inorder TraversalLeetCode
- Binary Tree Inorder Traversal leetcode javaLeetCodeJava
- LeetCode 94 [Binary Tree Inorder Traversal]LeetCode
- Leetcode 94. Binary Tree Inorder TraversalLeetCode
- Construct String from Binary TreeStruct
- LeetCode | 144. Binary Tree Preorder TraversalLeetCode
- Leetcode 144. Binary Tree Preorder TraversalLeetCode
- LeetCode144:Binary Tree Preorder TraversalLeetCode
- Binary Tree Inorder/Preorder Traversal 返回中序和前序/遍歷二叉樹的元素集合二叉樹
- leetcode [python] 【144】 Binary Tree Preorder TraversalLeetCodePython
- 【LeetCode 144_二叉樹_遍歷】Binary Tree Preorder TraversalLeetCode二叉樹
- LeetCode之Construct String from Binary Tree(Kotlin)LeetCodeStructKotlin
- Leetcode Binary Tree Postorder TraversalLeetCode
- LeetCode-Verify Preorder Serialization of a Binary TreeLeetCode
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- Leetcode Binary Tree Level Order TraversalLeetCode
- Leetcode-Binary Tree Postorder TraversalLeetCode
- Binary Tree Postorder Traversal leetcode javaLeetCodeJava
- LeetCode-Binary Tree Vertical Order TraversalLeetCode
- Leetcode Binary Tree Level Order Traversal IILeetCode
- Leetcode-Binary Tree Level Order TraversalLeetCode
- Binary Tree Level Order Traversal leetcode javaLeetCodeJava