LeetCode OJ 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.
通过树的中序序列和前序序列来构建一棵树。
例如:一棵树的前序序列是1-2-3,中序序列有5种可能,分别是1-2-3、2-1-3、1-3-2、2-3-1、3-2-1。不同的中序序列对应着不同的树的结构,这几棵树分别是[1,null,2,null,3]/[1,2,3]/[1,null,2,3]/[1,2,null,null,3]/[1,2,null,3]。
那么如何通过前序和中序序列来构建树呢?
我们知道:
1.前序序列是先visit根节点,然后visit左子树,最后visit右子树。
2.中序序列是先visit左子树,然后visit根节点,最后visit右子树。
因此,前序序列的第一个节点是根节点。我们在中序序列中找到根节点的位置,那么中序序列中根节点前面的节点就是根节点左子树中的节点,根节点后面的节点就是根节点右子树的节点。在上面的例子中,比如前序:1-2-3,中序2-1-3。由前序知道,1是根节点,在中序序列中找到1的位置,1前面的数是2,因此2是1的左子树,1的后面是3,因此3是1的右子树。所以对应是树为[1,2,3]。
知道了如何构建树,对应的代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length==0 || inorder.length==0) return null; TreeNode root = new TreeNode(preorder[0]);
int i;
for(i=0; i<inorder.length; i++){ //找到根节点在中序序列中的位置
if(inorder[i] == preorder[0]) break;
}
int[] left, right, npreorder;
if(i>0){
left = new int[i]; //左子树中的节点
System.arraycopy(inorder, 0, left, 0, left.length);
}else left = new int[0];
if(inorder.length - i -1 > 0){ //右子树中的节点
right = new int[inorder.length - i -1];
System.arraycopy(inorder, i+1, right, 0, right.length);
}else right = new int[0]; npreorder = new int[preorder.length - 1];//删除根节点
System.arraycopy(preorder, 1, npreorder, 0, npreorder.length);
root.left = buildTree(npreorder, left); //构建左子树
npreorder = new int[preorder.length - left.length - 1];//删除左子树中节点
System.arraycopy(preorder, 1 + left.length, npreorder, 0, npreorder.length);
root.right = buildTree(npreorder, right);//构建右子树
return root;
}
}
LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【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 OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
- 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- LeetCode OJ: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 由先序和中序遍历建立二叉树
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 (用先序和中序树遍历来建立二叉树)
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
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 ...
随机推荐
- Oracle中 union 和 union all 的区别
如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字. union(或称为联合)的作用是将多个结果合并在一起显示出来. union和uni ...
- vc6.0调试
调试快捷键 : 逐过程调试-F10 逐语句调试-F11跳到光标处-Ctrl+F10 跳出本循环-Shift+F11 设定断点-F9 删除所有断点-Ctrl+Shift+F9 ...
- 淘淘商城_day10_课堂笔记
今日大纲 Dubbo入门学习 使用dubbo优化单点登录系统 系统间服务调用方式 浏览器直接访问 浏览器发起请求,通过ajax或jsonp方式请求: Httpclient方式 系统与系统之间通过Htt ...
- hitTest和pointInside和CGRectContainsPoint
很多app中TabBar中间会有个凸起超出部分,为了点击超出父视图但是还想让按钮响应 //重写hitTest方法,去监听发布按钮的点击,目的是为了让凸出的部分点击也有反应- (UIView *)hit ...
- 简单的interface显式和隐式的实现
一,新建接口 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// < ...
- linux文件系统拓展属性
在研究GlusterFS中,发现GlusterFS使用了文件系统的Extended Attributes,中文可以称之为文件系统扩展属性.由于资料比较少,中文资料更少,因此把记录几点Extended ...
- Struts对输入数据的校验
当我们在登录或者是注册时需要对用户输入的数据验证,以前都是浏览器传送数据到后台,后台对数据进行校验,如果有错误就带着错误信息转发带登录或者注册页面, struts可以简便的对输入数据进行校验 首先我们 ...
- HDU 2614 Beat(DFS)
题目链接 Problem Description Zty is a man that always full of enthusiasm. He wants to solve every kind o ...
- Gentoo安装详解(三)-- 配置系统
配置系统 系统信息: 文件系统信息: 创建/etc/fstab nano -w /etc/fstab 网络信息: Host name, Domainname, etc nano -w /etc/con ...
- 网络通信框架Apache MINA
Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...