Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:linux 目录空间查看 编辑:程序博客网 时间:2024/06/10 23:28
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
#include<iostream>#include<vector>#include<algorithm>using namespace std;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};TreeNode *CreatTree(vector<int>::iterator in_begin, vector<int>::iterator in_end, vector<int>::iterator pos_begin, vector<int>::iterator pos_end ){if (in_begin == in_end)return NULL;vector<int>::iterator Rootpos = pos_end-1;vector<int>::iterator Rootino = find(in_begin, in_end, *Rootpos);auto lenleft = distance(in_begin, Rootino);TreeNode *root = new TreeNode(*Rootpos);root->left = CreatTree(in_begin, Rootino, pos_begin, pos_begin + lenleft);root->right = CreatTree(Rootino + 1, in_end, pos_begin + lenleft, Rootpos);return root;}TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {if (inorder.empty())return NULL;TreeNode *Root = new TreeNode(0);Root = CreatTree(inorder.begin(), inorder.end(), postorder.begin(), postorder.end());return Root;}
0 0
- *(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 Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- VC6.0下设置Unicode编译环境
- 百度ueditor1.4.3 jsp版使用注意事项
- Ubuntu Phone开箱上手
- (4.1.18.3)Android Service 服务(二)—— BroadcastReceiver
- Content Provider之Calendar And Contact Provider
- Construct Binary Tree from Inorder and Postorder Traversal
- 题目1520:树的子结构
- 割接注意点
- 关于viewPager matrix 计算 ,现在记录下来
- c++的类和对象
- 如何查看linux系统下的各种日志文件 linux 系统日志的分析大全
- vi文本插入命令汇总
- (4.1.18.4)Android Service 服务(三)—— bindService与remoteService
- 有关内存的思考题