Leetcode #106 Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:出售数据资源 编辑:程序博客网 时间:2024/05/21 17:42
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Difficulty:Medium
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* build(vector<int>& postorder, vector<int>& inorder,int pStart,int pEnd,int iStart, int iEnd){ //cout<<pEnd<<endl; TreeNode* root; if(pEnd<pStart) return NULL; int mid = postorder[pEnd]; root = new TreeNode(mid); if(pEnd == pStart) return root; int p_l_s,p_l_e,p_r_s,p_r_e; int i_l_s,i_l_e,i_r_s,i_r_e; i_l_s = iStart; i_l_e = iStart; while(inorder[i_l_e]!=mid) i_l_e++; i_l_e--; p_l_s = pStart; p_l_e = p_l_s + (i_l_e - i_l_s); p_r_s = p_l_e+1; p_r_e = pEnd-1; i_r_s = i_l_e + 2; i_r_e = iEnd; root->left = build(postorder,inorder,p_l_s,p_l_e,i_l_s,i_l_e); root->right = build(postorder,inorder,p_r_s,p_r_e,i_r_s,i_r_e); return root; } TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { if(postorder.size()==0) return NULL; return build(postorder,inorder,0,postorder.size()-1,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: Construct Binary Tree from Inorder and Postorder Traversal [106]
- 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
- Construct Binary Tree from Inorder and Postorder Traversal - LeetCode 106
- LeetCode 106:Construct Binary Tree from Postorder and Inorder 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
- iPhone开发入门系列2(iOS8+Swift版)备忘录APP学习01
- 犀牛——第7章数组 7.6 数组遍历ssh使用
- 已知二叉树的后序和中序排列,求先序排列再线索化的问题(笔试常考)
- 一定用好自己的应届毕业生身份
- Leetcode #105 Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode #106 Construct Binary Tree from Inorder and Postorder Traversal
- iPhone入门开发系列2备忘录APP学习02
- Ui 篇:ScrollViewFroLinearLayout
- 点滴记录一些shell命令
- 移动“芯”软:IoT与数据中心或成蓝海
- ConcurrentHashMap的使用 水太深了
- 警示新能源车自燃 工信部排查安全隐患
- ASP.Net 文件存取拒绝
- PyQt5初级教程--PyQt5中的布局管理[5/13]