【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 ...
随机推荐
- AC日记——让我们异或吧 洛谷 P2420
题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...
- E. Sergey and Subway
比赛时候写复杂了…… 我写的是 计算每个节点树内所有点到某个点的距离和. #include <bits/stdc++.h> using namespace std; typedef lon ...
- iNeuOS云操作系统,.NET Core全系打造
iNeuOS云操作系统,.NET Core全系打造 目录 一.演示地址... 2 二.技术体系... 2 三.iNeuOS整体介绍... 2 四.iNeuView概述... 3 五.iNeuView操 ...
- rostopic pub
rostopic pub -1 reinit_motor_wheel std_msgs/String -- "reinit_motor_wheel"rostopic pub -r ...
- django使用logging记录日志
django使用logging记录日志,我没有用这方式去记录日志,主要还是项目小的原因吧, 有机会遇见大项目的话可以回头研究. 配置setting.py配置文件 import logging impo ...
- Netty----start
一直欠着netty的帐,这次开始换上,netty 开始,学习的话,直接搞个源码的例子 http://central.maven.org/maven2/io/netty/netty-example/ ...
- SG函数学习总结
有点散乱, 将就着看吧. 首先是博弈论的基础, 即 N 和 P 两种状态: N 为必胜状态, P 为必败状态. 对于N, P两种状态, 则有 1. 没有任何合法操作的状态, P; 2. 可以移动到P局 ...
- Python之Django-part 1
python manage.py syncdb 在django1.7已经被取代了:用python manage.py migrate 代替来移动库: 删除.卸载django 在cd /usr/lo ...
- CheckedListBoxControl 或CheckedListBox 控件中显示水平滚动条 z
public partial class Form1 : Form { public Form1() { InitializeComponent(); DisplayHScroll(); } /// ...
- TCP通过滑动窗口和拥塞窗口实现限流,能抵御ddos攻击吗
tcp可以通过滑动窗口和拥塞算法实现流量控制,限制上行和下行的流量,但是却不能抵御ddos攻击. 限流只是限制访问流量的大小,是无法区分正常流量和异常攻击流量的. 限流可以控制本软件或者应用的流量大小 ...