重建二叉树

LeetCode-105

  • 首次需要知道前序遍历和中序遍历的性质。
  • 解题思路如下:首先使用前序比遍历找到根节点,然后使用中序遍历找到左右子树的范围,再分别对左右子树实施递归重建。
  • 本题的难点就是如何在前序遍历中找到左右子树的范围以分别重构,这可以根据中序遍历中的左右子树的数量来分辨。使用前序遍历的根节点和中序遍历的根节点相同时,表示此时前序遍历的左子树已经找到了。
  • 具体的实施时需要带有4个参数分别控制左右子树的范围。
/**
* 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。
* 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
**/
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/**
* [3,9,20,15,7]
* [9,3,15,20,7]
3
/ \
9 20
/ \
15 7
**/
class Solution {
private:
int sum=0;//
int l,r;
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
int length=preorder.size();
int startpre=0,startin=0,endpre=length-1,endin=length-1;
return buildTree(preorder,inorder,startpre,endpre,startin,endin);
}
//每次根据当前子树的前序以及后序遍历的位置来重建左右子树
TreeNode* buildTree(vector<int>& preorder,vector<int>& inorder,int startpre,int endpre,int startin,int endin){
if(startpre>endpre||startin>endin)
return NULL;
int discrepancy=0;//表示从中序遍历中找到的分割左右子树的位置
while(inorder[startin+discrepancy]!=preorder[startpre]){
discrepancy++;
}
TreeNode* node=new TreeNode(preorder[startpre]);
node->left=buildTree(preorder,inorder,startpre+1,startpre+discrepancy,startin,startin+discrepancy-1);
node->right=buildTree(preorder,inorder,startpre+discrepancy+1,endpre,startin+discrepancy+1,endin);
return node;
}
};
int main(){
TreeNode* t1=new TreeNode(10);
TreeNode* t2=new TreeNode(5);
TreeNode* t3=new TreeNode(15);
TreeNode* t4=new TreeNode(3);
TreeNode* t5=new TreeNode(7);
TreeNode* t6=new TreeNode(18);
t2->left=t4;t2->right=t5;
t3->left=t6;
t1->left=t2;t1->right=t3;
Solution solution; system("pause");
return 0;
}

LeetCode-重建二叉树(前序遍历+中序遍历)的更多相关文章

  1. Java实现 LeetCode 105 从前序与中序遍历序列构造二叉树

    105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中 ...

  2. [LeetCode]105. 从前序与中序遍历序列构造二叉树(递归)、108. 将有序数组转换为二叉搜索树(递归、二分)

    题目 05. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 题解 使用HashMap记录当前子树根节点在中序遍历中的位置,方便每次 ...

  3. LeetCode 中级 - 从前序与中序遍历序列构造二叉树(105)

    一个前序遍历序列和一个中序遍历序列可以确定一颗唯一的二叉树. 根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中 ...

  4. 【leetcode 105. 从前序与中序遍历序列构造二叉树】解题报告

    前往 中序,后序遍历构造二叉树, 中序,前序遍历构造二叉树 TreeNode* build(vector<int>& preorder, int l1, int r1, vecto ...

  5. Leetcode 105. 从前序与中序遍历序列构造二叉树

    题目链接 题目描述 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder ...

  6. LeetCode 105. 从前序与中序遍历序列构造二叉树(Construct Binary Tree from Preorder and Inorder Traversal)

    题目描述 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9, ...

  7. leetcode 105从前序与中序遍历序列构造二叉树

    方法一:直接使用复制的数据递归:O(n)时间,O(n)空间,不计算递归栈空间: /** * Definition for a binary tree node. * struct TreeNode { ...

  8. Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树

    Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树 Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序 ...

  9. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++

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

  10. LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium

    要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...

随机推荐

  1. bzoj3626: [LNOI2014]LCA (树链剖分+离线线段树)

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1. 设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先. ...

  2. C# List.Sort与IComparer接口及Comparison委托应用于集合排序

    C#里List.Sort的用法 using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  3. css整理之-----------选择器

    背景 在20年初时总感觉自己的css 不够用,想把css 相关的东西整理下,去年一整年都比较忙,忙着就到2021了,今天趁着有点时间,先从选择器开始吧. 听说图片可以提升颜值.... 选择器 CSS选 ...

  4. docker的FAQ

    1.Docker能在非Linux平台(Windows+MacOS)上运行吗? 答:可以 2 .如何将一台宿主机的docker环境迁移到另外一台宿主机? 答:停止Docker服务,将整个docker存储 ...

  5. 注意力(Attention)与Seq2Seq的区别

    什么是注意力(Attention)? 注意力机制可看作模糊记忆的一种形式.记忆由模型的隐藏状态组成,模型选择从记忆中检索内容.深入了解注意力之前,先简要回顾Seq2Seq模型.传统的机器翻译主要基于S ...

  6. 【非原创】LightOJ - 1284 Lights inside 3D Grid【概率期望】

    学习博客: 戳这里 戳这里 戳这里 戳这里 题意: 在一个三维的空间,每个点都有一盏灯,开始全是关的, 现在每次随机选两个点,把两个点之间的全部点,开关都按一遍:问k次过后开着的灯的期望数量: 题解: ...

  7. web 存储方式汇总:Cookies,Session, Web SQL; Web Storage(LocalStorage ,SessionStorage),IndexedDB,Application Cache,Cache Storage

    1 1 1 web 存储方式汇总: 旧的方式: Cookies; Session; Web SQL; 新的方式 HTML5 : Web Storage(LocalStorage ,SessionSto ...

  8. how to read the 10th line of a text using shell script

    how to read the 10th line of a text using shell script shell script / bash script question https://l ...

  9. You Don't Know Chrome Features

    You Don't Know Chrome Features URL auto convert to QR Code click the tab URL address click QRCode ic ...

  10. js currying function All In One

    js currying function All In One js 实现 (5).add(3).minus(2) 功能 例: 5 + 3 - 2,结果为 6 https://stackoverflo ...