题目

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. HTML 5的革新——语义化标签(二)

    HTML 5的革新之一:语义化标签二文本元素标签.分组元素标签. HTML 5的革新——语义化标签(一)中介绍了一些HTML5新加的一些节元素,一张页面中结构元素构成网页大体,但是也需要其他内容来填充 ...

  2. ListBox,CheckBoxList,DropDownList,RadioButtonList的常见数据绑定

    ListBox,CheckBoxList,DropDownList,RadioButtonList的常见用法 四个都是选择控件,用法大同小异,基本都是指定键值对: 直接加选择项: void way1( ...

  3. javascript 转化一个数字数组为function数组(每个function都弹出相应的数字)

    javascript 转化一个数字数组为function数组(每个function都弹出相应的数字) var arrNum = [2,3,4,5,6,10,7]; var arrFun = []; f ...

  4. 设置Eclipse可以Debug模式调试JDK源码,并显示局部变量的值

    最近突然萌发了研究JDK源码的想法,所以就想到了在自己常用的Eclipse上可以调试JDK源码. 整个设置过程也很简单: 首先你要安装好JDK(我的JDK安装路径根目录是D:\Java\jdk-8u9 ...

  5. poj3046

    dp,可以再优化. #include <iostream> #include <cstdio> #include <cstring> using namespace ...

  6. IOS之TextView属性设置

    UIFontDescriptor *bodyFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFont ...

  7. Python3实现自动备份

    需求 将重要文件备份到指定目录,存档文件名称为“当前日期.zip”. 前提 1) Windows系统 2) Python 3以上版本 旗舰版 #!usr/bin/python # -*- coding ...

  8. Exoplanet: The hunt is on

    原文 How many planets are out there? Today scientists believe that planets could outnumber the stars.F ...

  9. Evaluate|GC content|Phred|BAC|heterozygous single nucleotide polymorphisms|estimate genome size|

    (Evaluate):检查reads,可使用比对软件:使用SOAPaligner重新排列:采用massively parallel next-generation sequencing technol ...

  10. BXS入门赛部分writeup

    pwn1  盲打(笑) 前言:没有听鱼哥的话,事先没有装好环境,于是开始没做出来,然后全程在装pwntools,经过一番努力,失败了0.0 最终在网上搜了一段python socket连接脚本,终于可 ...