Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)
根据中序和后续遍历构建二叉树。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder==null&&postorder==null||(inorder.length==0&&postorder.length==0)) return null;
return build(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
} public TreeNode build(int[] inorder,int inStart,int inEnd,int[] postorder,int postStart,int postEnd){
if(inStart>inEnd||postStart>postStart) return null;
TreeNode root=new TreeNode(postorder[postEnd]); //后续遍历的最后一个是根节点
//在中序遍历中找到根节点的位置
int i;
for( i=inStart;i<=inEnd;i++){
if(inorder[i]==postorder[postEnd]) break;
} root.left=build(inorder,inStart,i-1,postorder,postStart,postStart+i-inStart-1);
root.right=build(inorder,i+1,inEnd,postorder,postStart+i-inStart,postEnd-1);
return root;
}
}
Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)的更多相关文章
- [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 tha ...
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [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 that ...
- Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树
题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序 ...
- 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 ...
- 【题解二连发】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 ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- 01-Git简介和仓库创建
Git简介 Linus的第二个伟大作品.2005年由于BitKeeper软件公司对Linux社区停止了免费使用权.Linus迫不得己自己开发了一个分布式版本控制工具,从而Git诞生了. 目前使用Git ...
- SDL2源代码分析8:视频显示总结
===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...
- printf函数压栈解惑
最近看到一些程序员的笔试题目,经常会考到printf函数的参数压栈问题,总体来讲就是参数从右向左依次压栈,再出栈,但是今天看到一个看似很简单的题目,却一直找不到头绪.题目如下: #include &l ...
- 《java入门第一季》之tcp协议下的网络编程
tcp协议相对于udp更加安全. 首先看一下需求:服务器端开启,多个客户端同时向服务器发送数据,看哪个客户端先到达. 说明:这里我开启三个电脑实验,一台电脑写服务器端的程序,两台电脑开客户端的程序.服 ...
- HEVC(H.265)标准的编码器(x265,DivX265)试用
基于HEVC(H.265)的的应用级别的编码器发展的速度很快.所说的应用级别,就是指速度比较快的,有实际应用价值的编码器.目前可以直接使用的有两个:x265,DivX265. DivX265 DivX ...
- org/w3c/dom/ElementTraversal 错误解决办法
org/w3c/dom/ElementTraversal 错误解决办法 不记得之前几天把什么maven依赖包删除了,今天利用htmlunit运行代码的时候报了下面的错误: Exception in t ...
- linux 下停止java jar包 shell
linux 下停止java jar包 shell http://injavawetrust.iteye.com #!/bin/sh APP_HOME=/home/ap/injavawetrust/ba ...
- python的read() 、readline()、readlines()、xreadlines()
先来一个小例子: import sys dir= os.path.dirname(os.path.abspath(__file__)) file_path='%s/test.txt' % dir f ...
- Google地球查看香港地形
12月1号要去香港.为此需要先了解一下香港的情况.以前只知道用Google地球看别人照的照片(或者是全景照),这次无意间发现了它一个很强大的功能:立体的呈现一个地区的地形.对于像香港这样的多山地区来说 ...
- Android官方技术文档翻译——Eclilpse项目迁移
本文译自Android官方技术文档<Migrating From Eclipse Projects>,原文地址:http://tools.android.com/tech-docs/new ...