【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 duplicates do not exist in the tree.
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length==0||inorder.length==0)
return null;
return BuildTreeNode(preorder,0,preorder.length-1,inorder,0,inorder.length-1); } private TreeNode BuildTreeNode(int[] preorder, int prestart, int preend, int[] inorder,
int instart, int inend) {
if(prestart>preend||instart>inend)
return null;
int temp = preorder[prestart];
TreeNode root = new TreeNode(temp);
int leftlength=0; for(int i=instart;i<=inend;i++){
if(inorder[i]!=temp)
leftlength++;
else if(inorder[i]==temp)
break;
}
root.left=BuildTreeNode(preorder, prestart+1, prestart+leftlength, inorder, instart,instart+leftlength-1);
root.right=BuildTreeNode(preorder,prestart+leftlength+1,preend,inorder,instart+leftlength+1,inend);
return root;
}
}
【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【Leetcode】【Medium】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 ...
- 【树】Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后 ...
- 【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★
1.题目 2.思路 3.java代码 //测试 public class BuildTreeUsingInorderAndPreorder { public static void main(Stri ...
- [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 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刷题笔记】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 ...
随机推荐
- LeetCode OJ--Permutations *
https://oj.leetcode.com/problems/permutations/ 写出一列数的全排列 #include <iostream> #include <vect ...
- SYN攻击SYN Attack
SYN攻击SYN Attack SYN Attack是一种DOS攻击方式.它利用的是TCP协议的漏洞,攻击目标,使其不在响应网络请求.在TCP协议中,需要三次握手,才能建立TCP连接.在握手过程中 ...
- Oracle hidden costs revealed, Part2 – Using DTrace to find why writes in SYSTEM tablespace are slower than in others
http://blog.tanelpoder.com/2008/09/02/oracle-hidden-costs-revealed-part2-using-dtrace-to-find-why-wr ...
- 类加载器在加载类 的时候就已经对类的static代码块和static变量进行了初始化
类装载器ClassLoader 类装载器工作机制 类装载器就是寻找类的节码文件并构造出类在JVM内部表示对象的组件.在Java中,类装载器把一个类装入JVM中,要经过以下步骤: [1.]装载:查找和导 ...
- Android开发——内存优化 图片处理
8. 用缓存避免内存泄漏 很常见的一个例子就是图片的三级缓存结构,分别为网络缓存,本地缓存以及内存缓存.在内存缓存逻辑类中,通常会定义这样的集合类. private HashMap<Strin ...
- hough变换检测直线和圆
图像测量和机器视觉作业: 提取图像中的直线和点的位置坐标,将其按一定顺序编码存入一文本文件,并在原图像上叠加显示出来. 下午实验了一下: 程序环境:vs2013(活动平台为x64)+opencv3.1 ...
- java 图片加水印,设置透明度。说明非常具体
package com.yidao.common; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.aw ...
- 数组全排列 knuth 分解质因数
template<typename T> void swap(T* a, T* b) { T temp = *a; *a = *b; *b = temp; } //数组的全排列 void ...
- 从SDCard获取的图片按分辨率处理的方法
前段时间公司开发的Launcher要做主题切换的功能,但切换主题时须要从sdcard中获取要切换的图片资源,拿到后图片的大小不正常. 后来查找原因是:系统对不同分辨率拿到的图片资源会自己主动的做转化, ...
- Go语言阅读小笔记,来自知呼达达关于unsafe.Pointer的分享.
第一式 - 获得Slice和String的内存数据 func stringPointer(s string) unsafe.Pointer { p := (*reflect.StringHeader) ...