106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:java免费报表开发工具 编辑:程序博客网 时间:2024/06/07 07:25
- /**
- * Definition for binary tree
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- void createTree(TreeNode *p, vector<int> &inorder, int istart, int iend, vector<int> &postorder, int pstart, int pend, int flag){
- if(pstart > pend) return;
- p = new TreeNode(postorder[pend]);
- if(root == NULL) root = pNode = p;
- else{
- if(flag) pNode->right = p; // 将这颗树连接起来
- else pNode->left = p;
- pNode = p;
- }
- if(pstart == pend) return;
- int i = istart;
- for(; i<= iend; i++){
- if(inorder[i] == postorder[pend]) break;
- }
- int k = i - istart;
- createTree(p->left, inorder, istart, i-1, postorder, pstart, pstart+k-1, 0); // 创建左子树
- pNode = p;
- createTree(p->right, inorder, i+1, iend, postorder, pstart+k, pend-1, 1); // 创建右子树
- }
- TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
- root = pNode = NULL;
- int n = inorder.size();
- if(n == 0) return root;
- createTree(root, inorder, 0, n-1, postorder, 0, n-1, 0);
- return root;
- }
- private:
- TreeNode *root, *pNode;
- };
阅读全文
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
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 浅析重载、隐藏与覆盖
- 通过FileZilla连接VirtualBox 下的Centos7
- 605. Can Place Flowers
- java实现细胞自动机
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 利用Python进行数据分析.pdf
- 二叉树递归与非递归遍历
- java 回调机制
- java新特性——读写锁ReadWriteLock
- 第一百三十四天 : Puppet 的使用与进阶
- JavaSwing_1.6: CardLayout(卡片布局)
- MYSQL字符集与校对规则
- codeviz安装记录和使用实例