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 ...
随机推荐
- python join()阻塞的用法
join()阻塞的用法,用来检测线程有没有完全执行完毕 #!/usr/bin/env python#-*- coding:utf-8 -*-import threadingimport time de ...
- 解决ubuntu 里面vi的时候上下左右是ABCD删除也不起作用
解决ubuntu 里面vi的时候上下左右是ABCD,backspace也不起作用 cp /etc/vim/vimrc ~/.vimrc 用remove vim-common然后再install v ...
- eclipse保存时自动格式化代码和优化导包
- vsphere安装虚拟机
新建虚拟机完成后,启动虚拟机,打开启动虚拟机控制台,选择CD/DVD驱动器 选择iso镜像,可以是本地或存储中心的iso,选择后点击虚拟机--客户机--发送Ctrl+Alt+Del,接下来就是正常的操 ...
- Gdiplus 贴图(助记) -------------------从资源中载入PNG图片
从资源中载入图片,亦可改为从内从中加载: void LoadResImage(int nResID,Image * &lpImage) { HINSTANCE hIns=AfxGetInsta ...
- DOM对象和JQuery有什么不同的地方?
jQuery对象和DOM对象使用说明,需要的朋友可以参考下.1.jQuery对象和DOM对象第一次学习jQuery,经常分辨不清哪些是jQuery对象,哪些是 DOM对象,因此需要重点了解jQuery ...
- URAL 6089 Nine
水题,找误差范围之内9最多的时间,如果有多个,选择误差最小的一个,如果还有多个,选择字典序最小的一个.同一个时间可以有不同的表示方法,例如60:15也可以表示为59:75. #include<s ...
- Flood-it!
Flood-it! 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4127/http://acm.split.hdu.edu.cn/showproble ...
- [妙味JS基础]JS热身运动
知识点总结 获取ID元素 document.getElementById(' ') 事件:鼠标事件.键盘事件.系统事件.表单事件.自定义事件 onclick onmouseout onmouseove ...
- 3、Spring的AOP详解和案例
AOP(Aspect Oriented Programming),即面向切面编程. 1.OOP回顾 在介绍AOP之前先来回顾一下大家都比较熟悉的OOP(Object Oriented Programm ...