LeetCode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
题意:已经二叉树的中序遍历和后序遍历,求二叉树的结构
思路:通过后序遍历中的最后一个元素,然后通过中序遍历知道左子树的包含元素和右子树的包含元素。根据左子树的中序遍历和后序遍历构造左子树,右子树的中序遍历和后序遍历构造右子树。
代码如下:
//二叉树的结构表示为
class TreeNode
{int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}
}
class Solution
{public TreeNode buildTree(int[] inorder, int[] postorder){int len = postorder.length;if (0 == len) return null;TreeNode root = new TreeNode(postorder[len - 1]);int index = 0;for (int i = 0; i < inorder.length; i++){if (inorder[i] == postorder[len - 1]){index = i;break;}}int[] leftinorder = new int[index];int[] leftpostorder = new int[index];int[] rightinorder = new int[len - index - 1];int[] rightpostorder = new int[len - index - 1];for (int i = 0; i < index; i++){leftinorder[i] = inorder[i];leftpostorder[i] = postorder[i];}for (int i = 0; i < len - index - 1; i++){rightinorder[i] = inorder[index + 1 + i];rightpostorder[i] = postorder[index + i];}root.left = buildTree(leftinorder, leftpostorder);root.right = buildTree(rightinorder, rightpostorder);return root;}
}
LeetCode Construct Binary Tree from Inorder and Postorder Traversal相关推荐
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...
- 【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 -day23 Construct Binary Tree from Inorder and Postorder Traversal Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- LeetCode: 106. Construct Binary Tree from Inorder and Postorder Traversal
题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
- leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- LeetCode - 106 - Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- Construct Binary Tree from Inorder and Postorder Traversal
题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
- 【Construct Binary Tree from Inorder and Postorder Traversal】cpp
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
- leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal | 106. 从中序后序遍历序列构造二叉树(Java)
题目 https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题解 待优化: ...
最新文章
- linux c socket 服务器 客户端 简单通讯
- android不同机型上界面适配问题
- Lucky7(hdu5768)
- sqlserver 储存过程 批量更新_大白菜怎么储存过冬,好吃还不烂?掌握方法,其实很简单...
- x轴z轴代表的方向图片_游戏中到底是Z轴朝上还是Y轴朝上?
- Linux——VIM中代码的折叠和打开
- (转)Bootstrap 之 Metronic 模板的学习之路 - (4)源码分析之脚本部分
- 控制台——EventLog实现事件日志操作
- VS2012 单元测试之泛型类(Generics Unit Test)
- 10.27T1 堆贪心
- 谭浩强c语言程序设计试题汇编pdf,C语言程序设计基础(谭浩强)习题.pdf
- overleaf 插入visio图像
- 全国大学生电子设计竞赛(七)--逆变电源设计
- vray铺装材质参数设计蓝海创意云渲染
- 2016.12.30非线性优化计算方法1
- github协同工作流程(一)
- 文本数据挖掘-----词向量
- iperf的ios版本;无线局域网测速
- gethostbyname linux,gethostbyname()
- WPF教程(五) XAML是什么?
热门文章
- 关于页面加载的方法收集
- 使用jsp:useBean指令
- ASIHTTPRequest取消异步请求
- WPF and Silverlight 学习笔记(十八):WPF样式(Style)与模板(Template)
- php 报错乱码,thinkphp3 phpexcel 导出报错乱码清除ob
- mysql topn_TopN语句
- 基于python的游戏设计与实现-基于Python的飞机大战游戏系统设计与实现开题报告...
- python代码写完怎么运行-Python 项目代码写完了,然后怎么打包和发布?
- python怎么知道用哪个库使用-Python绘图库讲解:4大常用库你准备学哪个?
- python入门教程 官方-Python自学入门?