Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0105 - Construct Binary Tree from Preorder and Inorder Traversal

LeetCode 0105 - Construct Binary Tree from Preorder and Inorder Traversal

作者头像
Reck Zhang
发布于 2021-08-11 02:53:05
发布于 2021-08-11 02:53:05
20700
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Construct Binary Tree from Preorder and Inorder Traversal

Desicription

Given preorder and inorder traversal of a tree, construct the binary tree.

Note: You may assume that duplicates do not exist in the tree.

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * 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 {
private:
    TreeNode* generateTree(int preStart, int inStart, int inEnd, vector<int>& preorder, vector<int>& inorder) {
        if(preStart > preorder.size()-1 || inStart > inEnd)
            return NULL;
        TreeNode* root = new TreeNode(preorder[preStart]);
        int inIndex = 0;
        for(int i = inStart; i <= inEnd; i++) {
            if(inorder[i] == preorder[preStart]){
                inIndex = i;
                break;
            }
        }
        root->left = generateTree(preStart+1, inStart, inIndex-1, preorder, inorder);
        root->right = generateTree(preStart+inIndex-inStart+1, inIndex+1, inEnd, preorder, inorder);
        return root;
    }

public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        return generateTree(0, 0, inorder.size()-1, preorder, inorder);
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-12-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LeetCode 0106 - Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.
Reck Zhang
2021/08/11
1730
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 duplicates do not exist in the tree. 根据先序和中序遍历还原二叉树。 思路很简单,先序中的第一个点必然为root,所以只要以先序的第一个元素在中序中的位置为分界线,左边为左子树,右边为右子树,递归下去就行 然而我遇到了问题,这是第一个版本,通过MLE了,我想应该是
triplebee
2018/01/12
4560
Leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 中序和后序还原二叉树。 直接在上一题的代码上做一点小修改就行了。 /** * Definition for a binary tree node. * struct TreeNode { * int val;
triplebee
2018/01/12
6350
【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.
阳光岛主
2019/02/19
3990
Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree.
卡尔曼和玻尔兹曼谁曼
2019/01/22
3600
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
ShenduCC
2018/07/24
2800
Leetcode: Construct Binary Tree from Inorder and Postorder Traversal
题目: Given inorder and postorder traversal of a tree, construct the binary tree.
卡尔曼和玻尔兹曼谁曼
2019/01/22
4590
关关的刷题日记97 – Leetcode 105. Construct Binary Tree
关关的刷题日记97 – Leetcode 105. Construct Binary Tree 题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路 给出二叉树的先根遍历和中根遍历,构造二叉树。 了解先根遍历和中根遍历的特点。每次在先根遍历数组中最左边的就是二叉树的根,然后去中
WZEARW
2018/04/12
7550
[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal [LeetCode] Construct Bina
链接:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/ 难度:Medium 题目:105. Construct Binary Tree from Preorder and Inorder Traversal
尾尾部落
2018/09/04
3510
Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/82191052
Tyan
2019/05/26
4090
LeetCode 二叉树 题目分类汇总
简书的 markdown 都不支持 [TOC] 语法……我就不贴目录了。下面按照类别,列出了29道关于二叉树的题目。认真看会发现,其实题目核心思想都是DFS(如果使用的话),并且题目的类型并不多。
Yano_nankai
2018/10/08
1.4K0
【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
韩旭051
2020/06/22
3800
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.
ShenduCC
2018/07/24
3480
leetcode: 105. Construct Binary Tree from Preorder and Inorder Traversal
Difficulty Medium. Problem Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the follow
JNingWei
2019/02/25
4810
[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/ 难度:Medium 题目:106. Construct Binary Tree from Inorder and Postorder Traversal
尾尾部落
2018/09/04
5150
关关的刷题日记98 – Leetcode 106. Construct Binary Tree from Inorder
关关的刷题日记98 – Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路 给出二叉树的后根遍历和中根遍历,构造二叉树。 了解后
WZEARW
2018/04/12
7440
Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/82191058
Tyan
2019/05/25
3280
Leetcode 题目解析之 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
ruochen
2022/01/08
1.4K0
N叉树问题-LeetCode 429、589、590、105、106(构建二叉树,N叉树遍历)
给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。 例如,给定一个 3叉树 :
算法工程师之路
2019/11/26
1.2K0
N叉树问题-LeetCode 429、589、590、105、106(构建二叉树,N叉树遍历)
LeetCode 0094 - Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes’ values.
Reck Zhang
2021/08/11
1820
推荐阅读
相关推荐
LeetCode 0106 - Construct Binary Tree from Inorder and Postorder Traversal
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验