[LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
2015-04-17 17:19
471 查看
给定一棵树的先序遍历和中序遍历结果,重新构建这棵树。
解决思路:
1. 从先序遍历序列中找到root节点
2. 在中序遍历序列中找到root出现的下标位置,记为root_iter. root_iter左边的为左子树的中序遍历序列,长度为lTreeSize, 右边为右子树的中序遍历序列。
3. 先序遍历序列中,除了第一个元素root节点,剩下的lTreeSize个元素是左子树的先序遍历序列,最后剩下的就是右子树的中序遍历序列了。
4. 得到左右子树的先序,中序遍历序列后,递归构建左右子树。
编码上的坑:
vector的迭代器的end指向容器中最后一个元素的后一位,为一虚拟元素。左子树递归下标更新时需要注意。
解决思路:
1. 从先序遍历序列中找到root节点
2. 在中序遍历序列中找到root出现的下标位置,记为root_iter. root_iter左边的为左子树的中序遍历序列,长度为lTreeSize, 右边为右子树的中序遍历序列。
3. 先序遍历序列中,除了第一个元素root节点,剩下的lTreeSize个元素是左子树的先序遍历序列,最后剩下的就是右子树的中序遍历序列了。
4. 得到左右子树的先序,中序遍历序列后,递归构建左右子树。
编码上的坑:
vector的迭代器的end指向容器中最后一个元素的后一位,为一虚拟元素。左子树递归下标更新时需要注意。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ typedef vector<int>::iterator ITER; class Solution { public: TreeNode *buildTree_helper(ITER p_start, ITER p_end, ITER in_start, ITER in_end){ if(p_end == p_start) return NULL; if(in_start == in_end) return NULL; TreeNode *root = new TreeNode(*p_start); ITER root_iter = find(in_start, in_end, root->val); int lTreeSize = root_iter-in_start; root->left = buildTree_helper(p_start+1,p_start+lTreeSize+1,in_start,root_iter); root->right = buildTree_helper(p_start+lTreeSize+1,p_end,root_iter+1,in_end); return root; } TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { int n = preorder.size(); if(n==0) return NULL; return buildTree_helper(preorder.begin(),preorder.end(),inorder.begin(),inorder.end()); } };
相关文章推荐
- 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
- leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal