博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
力扣题解-105. 从前序与中序遍历序列构造二叉树(分治法思想,递归的方式求解)
阅读量:4300 次
发布时间:2019-05-27

本文共 2505 字,大约阅读时间需要 8 分钟。

题目:105. 从前序与中序遍历序列构造二叉树

根据一棵树的前序遍历与中序遍历构造二叉树。

注意:

你可以假设树中没有重复的元素。

例如,给出

前序遍历 preorder = [3,9,20,15,7]

中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:

3   / \  9  20    /  \   15   7

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

前序遍历序列的第一个元素即为根节点。

中序遍历序列的根节点位于中间,左边为其左子树构成的中序遍历序列,右边为其右子树构成的中序遍历序列。

根据左右子树的序列长度,就可以确定前序遍历序列的左右子树序列的分界点。

举例来说明分治法思想的运用。

前序遍历 preorder = [3,9,20,15,7]

中序遍历 inorder = [9,3,15,20,7]

  1. divide
    首先找出根节点,即preorder的元素3;然后找到元素3在inorder的位置,将inorder序列分为左子树序列 [9] 和右子树序列 [15,20,7];

此时根据inorder的左子树序列可以确定,将preorder分为左子树序列 [9] 和右子树序列 [20,15,7];

  1. conquer

    构造根节点root(3),然后递归地处理左子树序列和右子树序列,分别作为左子树left和右子树right。

  2. combine

    将二叉树拼接起来,即root->left = left,root->right = right, 返回root。

代码

/** * 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 {
vector
preorder; vector
inorder;public: TreeNode* buildTree(vector
& preorder, vector
& inorder) {
this->preorder = preorder; this->inorder = inorder; return dfs(0, preorder.size()-1, 0, inorder.size()-1); } TreeNode* dfs(int startPre, int endPre, int startIn, int endIn) {
if (startPre > endPre or startIn > endIn) {
return NULL; } if (startPre == endPre or startIn == endIn) {
return new TreeNode(preorder[startPre]); } TreeNode * root = new TreeNode(preorder[startPre]); int rootPos = findRootPosInorder(preorder[startPre], startIn, endIn); if (rootPos < 0) {
return NULL; } int leftStartIn = startIn; int leftEndIn = rootPos-1; int rightStartIn = rootPos+1; int rightEndIn = endIn; int leftSize = rootPos-startIn; int leftStartPre = startPre+1; int leftEndPre = startPre+leftSize; int rightStartPre = startPre + leftSize + 1; int rightEndPre = endPre; root->left = dfs(leftStartPre, leftEndPre, leftStartIn, leftEndIn); root->right = dfs(rightStartPre, rightEndPre, rightStartIn, rightEndIn); return root; } int findRootPosInorder(int value, int startIn, int endIn) {
for (int i = startIn; i <= endIn; i++) {
if (value == inorder[i]) {
return i; } } return -1; }};
你可能感兴趣的文章
Ubuntu安装指定版本的docker
查看>>
MySQL show processlist过滤
查看>>
Python日志logging的levelname格式化参数1.1s小记
查看>>
ubuntu虚拟机VMware桥接模式无法自动化获取IP的解决方法
查看>>
Python debug 报错:SystemError: unknown opcode
查看>>
Python将树结构转换成字典形式的多级菜单结构,写入json文件
查看>>
关闭linux防火墙让windows宿主机访问ubuntu虚拟机web服务以及docker
查看>>
pycharm 找不到同目录文件,但是终端中正常的小记
查看>>
安装了grpc但是无法导入:ImportError: No module named 'grpc'
查看>>
Python中logging模块的基本用法
查看>>
Python查看第三方库、包的所有可用版本,历史版本
查看>>
一键将Python2代码转成Python3小记,
查看>>
Python要求O(n)复杂度求无序列表中第K的大元素
查看>>
Python 各种进制互相转换的函数
查看>>
python的单例理解、__new__、新式类object以及python2和python3下__new__的区别。
查看>>
Python动态规划以及编辑距离——莱文斯坦距离小记
查看>>
pycharm控制台报错:xmlrpc.client.Fault: Fault 0: 'java.lang.NullPointerException
查看>>
Python打印二叉树的左视图、右视图
查看>>
OpenStack Mitaka Horizon 主题开发
查看>>
OpenStack Mitaka keystone 分页(pagination)实现
查看>>