LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:ping的端口号 编辑:程序博客网 时间:2024/06/04 19:03
题目:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
class Solution {public: TreeNode* f(vector<int>& inorder,int l1,int r1, vector<int>& postorder,int l2,int r2) { vector<int>::iterator re = find(inorder.begin()+l1,inorder.begin()+r1,*(postorder.begin()+r2)); int numl = re - inorder.begin()-l1, numr = inorder.begin()+r1-re; TreeNode* t = new TreeNode(*(re)); if(numl != 0) {t->left = f(inorder,l1,re-inorder.begin()-1,postorder,l2,l2+numl-1); } else { t->left = NULL; } if(numr != 0) { t->right = f(inorder,re-inorder.begin()+1,r1,postorder,r2-numr,r2-1); } else { t->right = NULL; } return t; } TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { if(inorder.size() == 0||postorder.size() == 0) return NULL; return f(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1); }};
阅读全文
0 0
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode --- 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106.Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-106.Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode - 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode-106. Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
- Qt程序无法启动,debug时提示During startup program exited with code 0xc0000135
- SQL注入
- 多文件编译1
- hdu3294---Girls' research
- MyBatis 笔记(四)——实体类属性和表字段的映射
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- Linux字符设备驱
- JVM_Java内存模型
- 面向对象的综合应用解析
- 人工神经网络:反向传播算法的数学基础(Part4)
- linux 下怎么设置终端快捷键
- Java中字段、属性、成员变量、局部变量、实例变量、静态变量、类变量、常量
- 字符流中第一个不重复的字符
- 【JZOJ5332】【NOIP2017提高A组模拟8.23】密码