Construct Binary Tree from Inorder and Postorder Traversal——LeetCode
Given inorder and postorder traversal of a tree, construct the binary tree.
题目大意:给定一个二叉树的中序和后续序列,构建出这个二叉树。
解题思路:首先后序序列的最后一个是根节点,然后在中序序列中找到这个节点,中序序列中这个节点左边的是根节点的左子树,右边的是右子树,由此递归构建出完整的树。
Talk is cheap:
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder == null || postorder == null) {
return null;
}
int inLen = inorder.length;
int postLen = postorder.length;
if ((inLen == 0 && postLen == 0) || inLen != postLen) {
return null;
} TreeNode root = new TreeNode(postorder[postLen - 1]);
if (inLen == 1) {
return root;
}
int pos = 0;
for (int i = 0; i < inLen; i++) {
if (inorder[i] == postorder[postLen - 1]) {
pos = i;
break;
}
}
int[] inLeft = Arrays.copyOfRange(inorder, 0, pos);
int[] inRight = Arrays.copyOfRange(inorder, pos + 1, inLen);
int[] postLeft = Arrays.copyOfRange(postorder, 0, pos);
int[] postRight = Arrays.copyOfRange(postorder, pos, postLen - 1); root.left = buildTree(inLeft, postLeft);
root.right = buildTree(inRight, postRight);
return root;
}
Construct Binary Tree from Inorder and Postorder Traversal——LeetCode的更多相关文章
- Construct Binary Tree from Inorder and Postorder Traversal || LeetCode
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...
- 【题解二连发】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 ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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 ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
随机推荐
- Java方法-字符串
[Java字符串] 通过字符串函数 compareTo (string) ,compareToIgnoreCase(String) 及 compareTo(object string) 来比较两个字符 ...
- 怎么捕获和记录SQL Server中发生的死锁
我们知道,可以使用SQL Server自带的Profiler工具来跟踪死锁信息.但这种方式有一个很大的敝端,就是消耗很大.据国外某大神测试,profiler甚至可以占到服 务器总带宽的35%,所以,在 ...
- 分享一个nodejs写的小论坛
引言:作为一个前端小菜鸟,最近迷上了node,于是乎空闲时间,为了练练手写了一个node的小社区,关于微信小程序的,欢迎大家批评指导. 项目架构部分 一.前端架构 作为一个写样式也得无聊的前端狮,我偷 ...
- 【转】《我的WCF之旅》博文系列汇总
转自:http://www.cnblogs.com/artech/archive/2007/09/15/893838.html WCF是构建和运行互联系统的一系列技术的总称,它是建立在Web Serv ...
- SGU 124.Broken line
时间限制:0.25s 空间限制:4M 题意: 给出n条线段和一个点,保证所有线段平行X轴或Y,并且闭合成一个多边形.判断这个点的位置是在多边形上,还是多边形内,还是多边形外. solution: 由于 ...
- 调用数据库过程函数mysql
Connection conn=JdbcUtil.getConnection();//JdbcUtil是我写的获取connection的工具类 CallableStatement cast=conn. ...
- windows编程中 一些前缀区分 IDR和IDD
IDC_:控件的ID命名前缀(Control) IDM_:菜单的ID命名前缀(Menu) IDD_:对话框的ID命名前缀(Dialog) IDR_:资源的ID命名前缀(Resource) IDS_:字 ...
- IE6中的常见BUG与相应的解决办法
开发前端的同学一定都知道,IE6是兼容BUG最多的浏览器,它不支持PNG alpha通道暂且不论.其文档的解析理解规范也引起了诸多恼人的BUG,有时甚至让人感到绝望.本文主要讲解一些比较容易遇到的IE ...
- 装了SVN,你的关联图标变了没有?
装了SVN,你的关联图标变了没有? 开始合作之后,装上了SVN,非常高效,我在VS写了一部分的代码,上传之后,别人通过下载或是更新,就更新到了合作同伴的VS里,相当于大家在一个VS里写代码.和保强他们 ...
- QTP10破解
1. 下载QTP10.0版本 http://h30302.www3.hp.com/prdownloads/T6510-15063.zip?ordernumber=380454070&itemi ...