[LeetCode]105. Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:千牛自己装淘宝店铺 编辑:程序博客网 时间:2024/06/10 00:35
- Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:
You may assume that duplicates do not exist in the tree.
已经假定了二叉树中不存在重复的值!
/** * 比较简洁,但反复复制vector,浪费空间 *//** * 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* buildTree(vector<int>& preorder, vector<int>& inorder) { if(preorder.empty() || inorder.empty()) return NULL; TreeNode* root = new TreeNode(preorder[0]); int i = 0; for(; i < inorder.size(); ++i){ if(inorder[i] == root->val) break; } // root如果有左子树,长度为i if(i > 0){ vector<int> pre_left(preorder.begin()+1, preorder.begin()+1+i); vector<int> in_left(inorder.begin(), inorder.begin()+i); root->left = buildTree(pre_left, in_left); } // root如果有右子树 if(i < inorder.size() - 1){ vector<int> pre_right(preorder.begin()+1+i, preorder.end()); vector<int> in_right(inorder.begin()+i+1, inorder.end()); root->right = buildTree(pre_right, in_right); } return root; }};
/** * 直接传入索引,不用开辟新的vector,节省空间 */ /** * 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* buildTree(vector<int>& preorder, vector<int>& inorder) { if(preorder.empty() || inorder.empty()) return NULL; return build(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1); } // preorder[pre_left, pre_right] 左闭右闭区间 TreeNode* build(vector<int>& preorder, int pre_left, int pre_right, vector<int>& inorder, int in_left, int in_right){ TreeNode* root = new TreeNode(preorder[pre_left]); // 找到root在inorder中的位置索引 int i = in_left; for(; i <= in_right; ++i){ if(inorder[i] == root->val) break; } // 在这里已经保证了两个局部使用的vector不会为空,所以最开始不需要检查索引 // 如果存在左子树,长度为 i-in_left if(i > in_left){ root->left = build(preorder, pre_left+1, pre_left+i-in_left, inorder, in_left, i-1); } // 如果存在右子树 if(i < in_right){ root->right = build(preorder, pre_left+i-in_left+1, pre_right, inorder, i+1, in_right); } return root; }};
阅读全文
0 0
- 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
- 105. Construct Binary Tree from Preorder and Inorder Traversal LeetCode
- 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
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
- HBase的体系结构
- 使用 Sublime + PlantUML 高效地画图
- fresco加载本地图片
- Hdu6140 Hybrid Crystals(2017多校第8场)
- caffe制作lmdb数据以及均值(简单暴力流)
- [LeetCode]105. Construct Binary Tree from Preorder and Inorder Traversal
- jsp中的basePath和path
- 数据结构——查找(1)
- ElasticSearch配置
- Java习惯用法总结
- 输入一个字母,输出后五个字母
- 杀进程BAT写法
- Android日历开发详解
- eclipse 启动maven web项目