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: ...
随机推荐
- DOM - nodeType 的取值
DOM 中,共有 12 中不同类型的节点,nodeType 的取值以数值表示. 节点类型 描述 子节点 1 Element 表示元素. Element, Text, Comment, Proce ...
- NSDate和NSString的转换及判定是昨天,今天,明天
用于uidate,picker.. +(NSDate*) convertDateFromString:(NSString*)uiDate{ NSDateFormatter *formatter ...
- 纯命令行教你Cocoapods的安装和使用
关于cocoapods的介绍和作用,网上有很多大神介绍的比我清楚,建议去看一下唐巧的http://blog.devtang.com/blog/2014/05/25/use-cocoapod-to-ma ...
- UILabel的高度自适应
_content = [UILabel new]; _content.text = @"日落时分,沏上一杯山茶,听一曲意境空远的<禅>,心神随此天籁,沉溺于玄妙的幻境里.仿佛我就 ...
- Codeforces 475 D.CGCDSSQ
题目说了a的范围小于10^9次方,可实际却有超过的数据...真是醉了 算出以f[i]结尾的所有可能GCD值,并统计: f[i]可以由f[i-1]得出. /* 递推算出所有GCD值,map统计 */ # ...
- splice 操作符
几乎所有的数组操作都可用 splice 实现. 除了第一个参数,数组,为必须,其余的参数都不是必须的. splice ARRAY, OFFSET, LENGTH, LIST OFFSET 和 LENG ...
- spring + maven +testng 测试常见依赖包问题
java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource解决方法:添加缺少的jar包:commons-coll ...
- [翻译]jQuery十周年-John Resig
10th Anniversary of jQuery Today marks the 10th anniversary of the release of jQuery...[原文] 今天是jQuer ...
- WPF WebBrowser 不可见问题的解析[转]
问题概述: 1.在Xaml中加入WebBrowser(不论是WPF中的控件,还是Winform中的控件) 2.设置Window Background="Transparent" A ...
- html5本地数据库(一)
本地数据库 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important ...