Construct Binary Tree from Preorder and Inorder Traversal [LeetCode]
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: Just do it recursively.
TreeNode *build(vector<int> &preorder, int pstart, int pend, vector<int> &inorder, int istart, int iend) {
TreeNode * root = new TreeNode(preorder[pstart]);
int idx_root = -;
for(int i = istart; i <= iend; i ++) {
if(inorder[i] == preorder[pstart]){
idx_root = i;
break;
}
} if(idx_root == -)
return NULL; int left_size = idx_root - istart;
if(left_size > )
root->left = build(preorder, pstart + , pstart + left_size, inorder, istart, idx_root - ); int right_size = iend - idx_root;
if(right_size > )
root->right = build(preorder, pend - right_size + , pend, inorder, idx_root + , iend); return root;
} TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
if(preorder.size() == || inorder.size() == || preorder.size() != inorder.size())
return NULL; return build(preorder, , preorder.size() -, inorder, , inorder.size() - );
}
Construct Binary Tree from Preorder and Inorder Traversal [LeetCode]的更多相关文章
- Construct Binary Tree from Preorder and Inorder Traversal——LeetCode
Given preorder and inorder traversal of a tree, construct the binary tree. 题目大意:给定一个二叉树的前序和中序序列,构建出这 ...
- Construct Binary Tree from Preorder and Inorder Traversal leetcode java
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...
- 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】105. 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 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- iOS,iPhone各机型设备号,屏幕宽高,屏幕模式
//获取设备型号 NSString *DeviceModel= [[UIDevice currentDevice] model]; //获取设备系统版本号 NSString *DeviceIOSVer ...
- mongo基本语句
批量更新 db.test.updateMany({name:'test'},{$set:{value:1}}) 单更新 db.test.update({name:'test'},{$set:{valu ...
- [caffe]linux下安装caffe(无cuda)以及python接口
昨天在mac上折腾了一天都没有安装成功,晚上在mac上装了一个ParallelDesktop虚拟机,然后装了linux,十分钟就安装好了,我也是醉了=.= 主要过程稍微记录一下: 1.安装BLAS s ...
- NS_ENUM vs. NS_OPTIONS
NS_ENUM用于定义普通枚举值,NS_OPTIONS用于定义位移相关操作的枚举值: typedef NS_ENUM(NSUInteger, EOCConnectionState) { EOCConn ...
- windows 8.1无人值守安装
上个星期网上泄漏了微软最新的操作系统Windows 8.1,我便迫不及待的下载下来进行体验.发现其安装过程交互次数太多,太过漫长,遂研究了一下无人值守安装,现将成果记录如下. 一. 微软有专门的布署工 ...
- Asp.net 之Application
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Worker Thread
http://www.codeproject.com/Articles/552/Using-Worker-Threads Introduction Worker threads are an eleg ...
- Android 图片的放大缩小拖拉
package com.example.ImageView; import android.annotation.SuppressLint; import android.content.Contex ...
- 简单的计算最值的MapReduce程序
import java.io.IOException;import java.util.StringTokenizer;import java.util.*;import org.apache.had ...
- python走起之第五话
模块 1.自定义模块 自定义模块就是在当前目录下创建__init__.py这个空文件,这样外面的程序才能识别此目录为模块包并导入 上图中libs目录下有__init__.py文件,index.py程序 ...