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: 115870
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
解题思路:
修改下上题代码解决,JAVA实现如下:
static public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildTree(postorder, inorder, 0, postorder.length-1, 0, inorder.length-1);
}
static public TreeNode buildTree(int[] postorder, int[] inorder, int pBegin, int pEnd, int iBegin, int iEnd){
if(pBegin>pEnd)
return null;
TreeNode root = new TreeNode(postorder[pEnd]);
int i = iBegin;
for(;i<iEnd;i++)
if(inorder[i]==root.val)
break;
int lenLeft = i-iBegin;
root.left = buildTree(postorder, inorder, pBegin, pBegin+lenLeft-1, iBegin, i-1);
root.right = buildTree(postorder, inorder, pBegin+lenLeft, pEnd-1, i+1, iEnd);
return root;
}
Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- [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 ...
- (二叉树 递归) 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 ...
- 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 ...
- C#解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 ...
- 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
原题地址 二叉树基本操作 [ ]O[ ] [ ][ ]O 代码: TreeNode *restore(vector<i ...
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)
原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- 窗体皮肤实现 - 增加Toolbar的交互性
稍微改造一下,让交互性更好点.增加提示和动态效果. 控件实现内容: 1.加入Hint提示 2.加入了简易动画效果,鼠标进入和离开会有个渐变效果. 实现方案: 1.基类选用 2.Action的关联 3. ...
- Linux学习之十一-Linux字符集及乱码处理
Linux字符集及乱码处理 1.字符(Character)是各种文字和符号的总称,包括各国家文字.标点符号.图形符号.数字等.字符集(Character set)是多个字符的集合,字符集种类较多,每个 ...
- ZT:成熟是一种明亮而不刺眼的光辉
成熟是一种明亮而不刺眼的光辉, 一种圆润而不腻耳的音响, 一种不再需要对别人察言观色的, 一种终于停止向周围申诉求告的大气, 一种不理会哄闹的微笑, 一种洗刷了偏激的冷漠, 一种无需声张的厚实, 一种 ...
- Android设计中的尺寸问题
Android把屏幕大小分成四种:small, normal, large, xlarge; 屏幕密度分成:low(ldpi), medium(mdpi), high(hdpi), extra hig ...
- javascript获取星期
入门: var week = new Date().getDaty(); var ary = new Array("日","一","二",& ...
- java使用Runtime.exec()运行windwos dos或linux shell命令
使用Runtime.exec()运行windwos dos或linux shell命令,按实际情况具体测试 实例代码: package com.bookoo.test.command; imp ...
- 关于layui-layer独立组件--弹出层
官方下载文档链接:http://layer.layui.com/ =================================================================== ...
- python3 configparser对配置文件读写
import configparser #read data from conf filecf=configparser.ConfigParser()cf.read("biosver.cfg ...
- Bootstrap学习速查表(四) 栅格系统
Bootstrap框架的网格系统工作原理如下: 1.数据行(.row)必须包含在容器(.container)中,以便为其赋予合适的对齐方式和内距(padding).如: 2.在行(.row)中可以添加 ...
- ios开发之猜数字游戏
// // main.m // 猜数 // #import <Foundation/Foundation.h> #import "Guess.h" int main(i ...