【LeetCode】Construct Binary Tree from (Preorder or Postorder) and Inorder Traversal
来源:互联网 发布:淘宝客服期末考试试题 编辑:程序博客网 时间:2024/05/17 23:55
题目描述
Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Given inorder and postorder traversal of a tree, construct the binary tree.
Given inorder and postorder traversal of a tree, construct the binary tree.
给定先序和中序求后序或者给定后序中序求先序
总结
使用数组一下标效率会高一些下标的确定一定要正确。
注意返回空的情况。
在vec做参数时需要使用引用,否则系统会复制导致内存溢出
代码示例
class Solution {public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int size = inorder.size();if(size == 0) return NULL;return buildTreeCore(inorder,0,size - 1,postorder,0,size - 1); } //vector必须使用传引用,否则每一次递归都会进行一次复制,内存会超出 TreeNode *buildTreeCore(vector<int> &inorder,int instart,int inend,vector<int> &postorder,int poststart,int postend) { if(poststart > postend)////////////////////////////////!!!!!!!!!!!!!!!!!!! return NULL; TreeNode *root = new TreeNode(postorder[postend]); if(poststart == postend) //这里可以加一个报错,如果最后剩一个如果两个vector中val不一样就错了 return root; int pos = 0;for(int i = instart;i<=inend;i++)if(inorder[i] == postorder[postend]){ pos = i;break;} //开始和结束点需要计算清楚 if(pos>instart)root->left = buildTreeCore(inorder,instart,pos-1,postorder,poststart,poststart+(pos-instart)-1); if(inend>pos)root->right = buildTreeCore(inorder,pos+1,inend,postorder,poststart+(pos-instart),postend-1);return root;}};
class Solution {public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { int size = inorder.size();if(size == 0) return NULL;return buildTreeCore(preorder,0,size - 1,inorder,0,size - 1); } //vector必须使用传引用,否则每一次递归都会进行一次复制,内存会超出 TreeNode *buildTreeCore(vector<int> &preorder,int prestart,int preend,vector<int> &inorder,int instart,int inend) { if(prestart > preend)////////////////////////////////!!!!!!!!!!!!!!!!!!! return NULL; TreeNode *root = new TreeNode(preorder[prestart]); if(prestart == preend) //这里可以加一个报错,如果最后剩一个如果两个vector中val不一样就错了 return root; int pos = 0;for(int i = instart;i<=inend;i++)if(inorder[i] == preorder[prestart]){ pos = i;break;} //开始和结束点需要计算清楚 if(pos>instart)root->left = buildTreeCore(preorder,prestart+1,prestart+(pos-instart),inorder,instart,pos-1); if(inend>pos)root->right = buildTreeCore(preorder,prestart+(pos-instart)+1,preend,inorder,pos+1,inend);return root;}};
推荐学习C++的资料
C++标准函数库
http://download.csdn.net/detail/chinasnowwolf/7108919
在线C++API查询
http://www.cplusplus.com/
vector使用方法
http://www.cplusplus.com/reference/vector/vector/
0 0
- 【LeetCode】Construct Binary Tree from (Preorder or Postorder) and Inorder Traversal
- 【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder/Preorder and Inorder Traversal
- leetCode(18):Construct Binary Tree from Preorder and Inorder (Inorder and Postorder) Traversal
- LeetCode 105, 106. Construct Binary Tree from Preorder and Inorder/Inorder and Postorder Traversal
- 【Leetcode】【python】Construct Binary Tree from Preorder and Inorder Traversal (Inorder and Postorder)
- Construct Binary Tree from Inorder and Preorder(Postorder) Traversal
- Construct Binary Tree from Preorder (Postorder) and Inorder Traversal
- Construct Binary Tree from Inorder and Preorder(Inorder and Postorder) Traversal
- LeetCode 105/106 Construct Binary Tree from Preorder/Postorder and Inorder Traversal
- leetcode题解-105 && 106. Construct Binary Tree from Preorder (PostOrder) and Inorder Traversal
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder 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
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- ArcGIS教程:对镶嵌数据集进行色彩平衡
- C#语言之“string格式的日期时间字符串转为DateTime类型”的方法
- 设计模式(11)-结构型模式-Flyweight模式
- HDU 4565 So Easy! 递推数列+矩阵快速幂
- Asp.Net 上传图片并生成高清晰缩略图
- 【LeetCode】Construct Binary Tree from (Preorder or Postorder) and Inorder Traversal
- Android开发之在一个程序中启动另一个程序
- 异或运算
- 访问需要HTTP Basic Authentication认证的资源的各种语言的实现
- 自定义上传按钮样式html
- C语言学习(6)---内存分配
- Python删除冗余文件技巧
- 科技股泡沫微裂:技术红利用尽?什么是下一波?
- Kconfig文件的作用以及驱动添加进内核示例