LintCode 159:Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:word2010软件下载 编辑:程序博客网 时间:2024/05/27 00:31
Description:
Given preorder and inorder traversal of a tree, construct the binary tree.
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
1.对前序的观察:若a在b前被遍历,则a为b的父节点或a为b的左兄弟。
2.对中序的观察:若a在b前被遍历,则a在b左边。
一开始(错误的)思路:
一开始想到树结点是不保存父节点的,因而构建树的时候也要由上之下地构建(而不能回退),所以想一个个按前序数组中的数在中序数组中比较两个结点的位置来构建树。但发现,给定两个结点在前序、中序中的关系,也无法确定这两个结点的关系,所以这种思路失败。
正确的思路:
既然无法由上之下地构建树,那就只能采用递归。(讲道理以后树的问题应该第一个想到递归这种方法了。)
递归就是要把问题拆成小问题,树的递归就是把一棵树拆成左子树和右子树。
可以发现,前序数组的第一个数一定是树的根结点,而在中序数组中,它将左、右子树分割了开来。所以通过根结点,我们重新分别构建根结点的左、右子树的前序数组和中序数组,在新分解后的数组中,每个数的前后相对关系保持不变,并且,通过观察左右子树的前序、中序遍历,我们可以发现左右子树的结点的访问是可以完全分割开的,并没有交错的部分(先访问左子树的一个节点再访问右子树的一个节点再访问左子树这样)。
Note:
1.注意在拆分数组构建新数组时,不要把root结点放进去。
2.vector<int> (begin,end)会把begin(指针)所指向的内容放进去,不会把end所指向的内容放进去。
class Solution {/***@param preorder : A list of integers that preorder traversal of a tree*@param inorder : A list of integers that inorder traversal of a tree*@return : Root of a tree*/public:TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {// write your code hereif (preorder.size() <= 0 || inorder.size() != preorder.size())return nullptr;TreeNode* root = new TreeNode(preorder[0]);int i;for (i = 0;i<inorder.size();i++) {if (inorder[i] == preorder[0])break;}if (i >= inorder.size())return nullptr;vector<int> ino_left(inorder.begin(), inorder.begin() + i);vector<int> ino_right(inorder.begin() + i + 1, inorder.end());vector<int> pre_left;vector<int> pre_right;for (i = 1;i < ino_left.size() + 1;i++)pre_left.push_back(preorder[i]);for (i = ino_left.size() + 1;i < preorder.size();i++)pre_right.push_back(preorder[i]);root->left = buildTree(pre_left, ino_left);root->right = buildTree(pre_right, ino_right);return root;}};
阅读全文
0 0
- LintCode 159:Construct Binary Tree from Preorder and Inorder Traversal
- lintcode: Construct Binary Tree from Preorder and Inorder Traversal
- lintcode:Construct Binary Tree from Preorder and Inorder Traversal
- *[Lintcode]Construct Binary Tree from Preorder and Inorder Traversal
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal & Construct Binary Tree from Inorder and P
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- 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] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- 仿茄子快传的一款文件传输应用
- 前端中的异步问题
- mysql 5.5 安装配置方法图文教程
- SQL Server 2005各版本区别
- PPT的对齐方式
- LintCode 159:Construct Binary Tree from Preorder and Inorder Traversal
- Apriori Algorithm
- 铁塔融云!不规则扩散的 Splash 效果。非常炫、非常炫、非常炫
- 虚拟机原理
- JSP——表单的两种提交方式get和post的区别
- 吾爱卿
- Java程序的参数传递及项目打包成JAR
- Python实现自动挂机脚本(基础篇)
- ELF 文件的结构01