105. Construct Binary Tree from Preorder and Inorder Traversal (Tree; DFS)
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
root = NULL;
if(inorder.empty()) return root;
root = new TreeNode();
buildSubTree(preorder,inorder,,preorder.size()-,,inorder.size()-,root);
return root;
}
void buildSubTree(vector<int> &preorder, vector<int> &inorder, int preStartPos, int preEndPos,int inStartPos, int inEndPos, TreeNode * currentNode) //对于树的递归遍历,如果涉及数组,必定参数中要传递start,end,指明当前子树来源于数组的哪一部分
{
currentNode->val = preorder[preStartPos]; //前序遍历的第一个为根节点
//find root position in inorder vector
int inRootPos;
for(int i = inStartPos; i <= inEndPos; i++)
{
if(inorder[i] == preorder[preStartPos])
{
inRootPos = i;
break;
}
}
//分别递归处理左子树和右子树
//left tree:是总序遍历中根节点所在位置之前的部分,对应前序遍历根节点之后相同长度的部分
int newPrePos = preStartPos+max(inRootPos-inStartPos, );
if(inRootPos>inStartPos)
{
currentNode->left = new TreeNode();
buildSubTree(preorder,inorder,preStartPos+, newPrePos,inStartPos,inRootPos-,currentNode->left); //递归调用时,要传递新的start,end
}
//right Tree:是中序遍历根节点所在位置之后的一部分,对应前序遍历从末尾开始相同的长度
if(inRootPos < inEndPos)
{
currentNode->right = new TreeNode();
buildSubTree(preorder,inorder,newPrePos+, preEndPos,inRootPos+,inEndPos,currentNode->right); //递归调用时,要传递新的start,end
}
}
private:
TreeNode* root;
};
105. Construct Binary Tree from Preorder and Inorder Traversal (Tree; DFS)的更多相关文章
- 【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 ...
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 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 ...
- 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 ...
- 【题解二连发】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 ...
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- LeetCode_Construct Binary Tree from Preorder and Inorder Traversal
一.题目 Construct Binary Tree from Preorder and Inorder Traversal My Submissions Given preorder and ino ...
- [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 ...
- leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- New Concept English Two 15 37
listening speaking reading writing and translating $课文35 捉贼! 355. Roy Trenton used to drive a taxi. ...
- 基于 React + NodeJS + Express + MongoDB 开发的一个社区系统
还可以, 功能挺全的, 可以作为react开发入门项目 链接 线上站点: 源码地址:
- Django 之 分页
1. urs.py # coding:utf-8 from django.conf.urls import url import views urlpatterns = [ # 分页练习 url(r' ...
- ZetCode PyQt4 tutorial First programs
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- [QT_OPENCV] qt下opencv配置以及首个opencv工程
使用环境 : window版本 : win7 x64 QT : 5.8 32bit MinGW530 OpenCv : 3.2 opencv在qt下的环境配置: 在百度上百度了许多关于opencv环境 ...
- jenkins执行shell命令,有时会提示“Command not found”
这个问题其实就是环境变量没有配准确 (1)检查你在Jenkins中设置的maven是否准确,可以通过[new job]按钮查看新建job中是否有maven选项,没有就是你配置的不准确 如果你下载的插件 ...
- graphql-yoga interface && union 使用
接口就是一个约定,方便数据的约定,union 可以实现数据类型的共享,减少代码量 基本项目 参考 https://github.com/rongfengliang/graphql-yoga-doc ...
- 【转】Vim自动补全插件----YouCompleteMe安装与配置
原文网址:http://www.cnblogs.com/zhongcq/p/3630047.html 使用Vim编写程序少不了使用自动补全插件,在Linux下有没有类似VS中的Visual Assis ...
- bzoj3143游走
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3143 学到了无向图中点被经过的期望次数和边被经过的期望次数. 一个点被经过的期望次数 就是 ...
- 对类 sizeof
sizeof一个类的时候,都什么会被计算?静态成员会被计算进来么?如果这是一个子类,它的父类成员会被计算么? #include <iostream> using namespace std ...