[leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
2015-05-20 21:38
465 查看
题目:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
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.empty() || inorder.empty() || preorder.size() != inorder.size()) return NULL; return buildTreeCore(preorder, 0, inorder, 0, preorder.size()); } private: int search(vector<int>& vec, int start, int len, int val){//在vec中的从start开始的len个元素中查找val,若找到,返回val在preorder中的位置;若找不到,返回-1; int end = start + len; for (int i = start; i < end; ++i){ if (vec[i] == val) return i; } return -1; } TreeNode* buildTreeCore(vector<int>& preorder, int preStart, vector<int>& inorder, int inStart, int len){ if (0 == len || preStart + len > preorder.size() || inStart + len > preorder.size()) return NULL; TreeNode *root = new TreeNode(preorder[preStart]); int index = search(inorder, inStart, len, preorder[preStart]); if (-1 == index) return NULL; int leftLen = index - inStart;//左子树节点个数 int rightLen = len - leftLen - 1;//右子树节点个数 int leftPreStart = preStart + 1;//前序序列中左子树的开始位置 int leftInStart = inStart;//中序序列中左子树的开始位置 int rightPreStart = preStart + 1 + leftLen;//前序序列中右子树的开始位置 int rightInStart = inStart + leftLen + 1;//中序序列中右子树的开始位置 if(leftLen > 0) root->left = buildTreeCore(preorder, leftPreStart, inorder, leftInStart, leftLen); if(rightLen > 0) root->right = buildTreeCore(preorder, rightPreStart, inorder, rightInStart, rightLen); return root; } };
相关文章推荐
- leetcode第一刷_Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode | Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal 中前序构造BST
- [LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal
- 【LEETCODE】105-Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
- 【LeetCode笔记】Construct Binary Tree from Preorder and Inorder Traversal 前序、中序还原二叉树
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode_Construct Binary Tree from Preorder and Inorder Traversal
- Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- 【LeetCode-面试算法经典-Java实现】【105-Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)】
- LeetCode "Construct Binary Tree from Preorder and Inorder Traversal"
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode-Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- leetcode-Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode:Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode_Construct Binary Tree from Preorder and Inorder Traversal