题目

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节点,然后在中序序列中查找该值,元素左侧为左子树,右侧为右子树; 求出左子树个数count,在前序序列中 , 除去第一个节点,接下来的count个元素构成左子树的前序序列,其余的构成右子树的前序序列。

开始,没有采用迭代器,声明vector占用了大量空间,Memory Limit Exceeded。。。

代码为:

/**
* 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; //求树中节点个数
int size = preorder.size(); //先序遍历第一个节点为树的根节点
TreeNode *root = new TreeNode(preorder[0]); int pos = 0;
//在中序遍历结果中查找根节点
for (int i=0; i<size; ++i)
{
if (inorder[i] == preorder[0])
{
pos = i;
break;
}//if
}//for if (pos >= 0 && pos < size)
{
//则在inOrder中(0 , pos-1)为左子树中序遍历结果(pos+1,size-1)为右子树的中序遍历序列
//在preOrder中(1,pos)为左子树前序遍历结果(pos+1,size-1)为右子树前序遍历结果
vector<int> left_pre;
for (int j = 1; j <= pos; j++)
left_pre.push_back(preorder[j]); vector<int> left_in;
for (int j = 0; j < pos; ++j)
left_in.push_back(inorder[j]); root->left = buildTree(left_pre, left_in); //构造右子树
vector<int> right_pre , right_in;
for (int j = pos + 1; j < size; j++)
{
right_pre.push_back(preorder[j]);
right_in.push_back(inorder[j]);
} root->right = buildTree(right_pre, right_in);
}
return root;
}
};

然后,使用迭代器避免不必要的空间占用,AC~

AC代码

class Solution {
public: template <typename Iter>
TreeNode* make(Iter pre_begin, Iter pre_end, Iter in_begin, Iter in_end) { if (pre_begin == pre_end || in_begin == in_end)
return NULL; //先序遍历第一个节点为树的根节点
TreeNode *root = new TreeNode(*pre_begin); //在中序遍历结果中查找根节点
Iter iter = find(in_begin, in_end, *pre_begin); int count = iter - in_begin; if (iter != in_end)
{
//则在inOrder中(0 , pos-1)为左子树中序遍历结果(pos+1,size-1)为右子树的中序遍历序列
//在preOrder中(1,pos)为左子树前序遍历结果(pos+1,size-1)为右子树前序遍历结果 root->left = make(pre_begin + 1, pre_begin + count + 1, in_begin, iter); //构造右子树
root->right = make(pre_begin + count + 1, pre_end, iter + 1, in_end);
}
return root; } TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (preorder.empty() || inorder.empty())
return NULL; return make(preorder.begin(), preorder.end(), inorder.begin(), inorder.end());
}
};

GitHub测试程序源码

LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  1. 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

    Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and ...

  2. 105 + 106. Construct Binary Tree from Preorder and Inorder Traversal (building trees)

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

  3. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  4. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  5. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  6. 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

  7. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

  8. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  9. [LeetCode] 105. 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 ...

随机推荐

  1. 接口测试02 - 无法绕过的json解析

    概述: 先瞧一下什么是json.JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式. 它基于ECMAScript(w3c定制的js规范)的一个子集 ...

  2. js 正则验证url

    var reg = '[a-zA-z]+://[^\s]*';//正则var url = $('#add [name=notice_url]').val();if(url.length >0){ ...

  3. js实现屏幕自适应局部

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. mongodb 文本索引

    启用文本搜索: 最初文本搜索是一个实验性功能,但2.6版本开始,配置是默认启用的.但是,如果使用的是以前 MongoDB 的版本,那么必须启用文本搜索,使用下面的代码: >db.adminCom ...

  5. 使用Intellij IDEA搭建一个简单的Maven项目

    IntelliJ IDEA是Java最优秀的开发工具,它功能全面,提示比较智能,开发界面炫酷,新技术支持的比较迅速. 我使用了Eclipse快10年了,IntelliJ IDEA这么好用必须要试一试. ...

  6. table表格字母无法换行

    在项目中,用到的table比较多,本来布局挺好的,后来在td内写入英文字母,整个布局就乱了,会撑的很宽,不换行,后来才知道:一般字母的话会被浏览器默认是一个字符串或者说一个单词,所以不会自动换行. 于 ...

  7. 超简单!一步创建自己的wifi热点~

    还在用某某卫士.某某管家创建wifi热点,甚至被忽悠专门买一个随身wifi吗?现在答案明确了:你完全用不着它们了.因为有更简单的方法. 只需要两个bat文件.一个用来启动wifi热点,另一个用来关闭w ...

  8. GoAccess参数选项

    GoAccess - 1.2 Usage: goaccess [filename] [ options ... ] [-c][-M][-H][-q][-d][...]The following opt ...

  9. C# 分支语句 练习题

    1.“请输入年份:”(1-9999) “请输入月份:”(1-12) “请输入日期:”(要判断大小月,判断闰年) 判断输入的时间日期是否正确 bool dateISOK = false;//放置日期是否 ...

  10. SQL中的动态语句执行--exec(@sqlstr)

    begin drop table #tmptable declare @money ut_money set @money=1.2345 create table #tmptable ( je ut_ ...