Leetcode #105 Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:出售数据资源 编辑:程序博客网 时间:2024/05/17 13:11
Given preorder and inorder 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>& preorder, vector<int>& inorder,int pStart,int pEnd,int iStart, int iEnd){ TreeNode* root; if(pEnd<pStart) return NULL; int mid = preorder[pStart]; 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 + 1; p_l_e = p_l_s + (i_l_e - i_l_s); p_r_s = p_l_e+1; p_r_e = pEnd; i_r_s = i_l_e + 2; i_r_e = iEnd; root->left = build(preorder,inorder,p_l_s,p_l_e,i_l_s,i_l_e); root->right = build(preorder,inorder,p_r_s,p_r_e,i_r_s,i_r_e); return root; } TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { if(preorder.size()==0) return NULL; return build(preorder,inorder,0,preorder.size()-1,0,preorder.size()-1); }};
0 0
- LeetCode(105)Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal [105]
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode 105] Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode 105]Construct Binary Tree from Preorder and Inorder Traversal
- leetcode || 105、Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal LeetCode 105
- Leetcode[105]-Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode---(105)Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode #105 Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 105: Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105: Construct Binary Tree from Preorder and Inorder Traversal
- 【LEETCODE】105-Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- 一道题Wrong Answer之后该何去何从?
- 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 文件存取拒绝