LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:数据库证书有哪些 编辑:程序博客网 时间:2024/05/29 08:36
LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal
Description
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
解题思路
根据二叉树的前序遍历和中序遍历构造二叉树。因为题目中提示了节点没有重复的值,所以我们可以判断节点的值来找出根节点。前序遍历的第一个节点是当前子树的根节点,由此,我们可以找到根节点在中序便利中的位置root_i,把中序遍历分为两部分:这个根节点的左右子树的中序遍历。同时,前序遍历可以根据这个查找的偏移量root_i把前序遍历分为两部分:这个根节点的左右子树的前序遍历。这样就可以递归构造这个根节点的左右子树了。
Note:
vetctor容器中的assign()函数:
函数原型:
void assign(const_iterator first,const_iterator last);
void assign(size_type n,const T& x = T());
功能:
将区间[first,last)的元素赋值到当前的vector容器中,或者赋n个值为x的元素到vector容器中。注意是不含last这个元素的!这是cplusplus.com的说明链接
代码
个人github代码链接
/** * 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) { TreeNode* root; if(preorder.size() == 0 || inorder.size() == 0) return NULL; if(preorder.size() == 1) { root = new TreeNode(preorder[0]); return root; } root = new TreeNode(preorder[0]); int root_i = 0; vector<int>::iterator root_itr = inorder.begin(); for(root_itr; root_itr != inorder.end(); root_itr++){ if(preorder[0] == (*root_itr)){ break; } root_i ++; } vector<int> left_pre,right_pre,left_in,right_in; vector<int>::iterator itr = preorder.begin(); left_pre.assign(itr + 1, itr + root_i + 1); right_pre.assign(itr + root_i + 1, preorder.end()); itr = inorder.begin(); left_in.assign(itr,root_itr); right_in.assign(root_itr + 1, inorder.end()); root->left = buildTree(left_pre, left_in); root->right = buildTree(right_pre, right_in); return root; }};
0 0
- LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode OJ 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
- 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
- Genymotion在win10下不能启动的解决方案
- 浅谈ElasticSearch的嵌套存储模型
- 汉诺塔游戏规律,让汉诺塔沦为体力劳动!(后附汉诺塔解法Python源码)
- tomcat学习笔记之catalina.sh
- yii2.0 利用Excel类做导入导出
- LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal
- 浅谈浏览器内核
- C#读取Excel文件
- vaadin功能特性
- opencv 灰度图
- 抓包工具Charles 注册码/破解方法
- MySql 免安装版配置
- JQuery onload、ready使用
- ETL-关于缓慢变化维的几种常见解决方法: