leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal----- java
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
和上一道题基本一样
根据中序和后序遍历确定一个棵树。
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- public class Solution {
- public TreeNode buildTree(int[] inorder, int[] postorder) {
- int len = inorder.length;
- if( len == 0)
- return null;
- return helper(inorder,0,len-1,postorder,len-1);
- }
- public TreeNode helper(int[] inorder,int start,int end, int[] postorder, int pos ){
- if( pos < 0 || start > end)
- return null;
- TreeNode node = new TreeNode(postorder[pos]);
- int size = 0;
- for( int i = end;i >= start && inorder[i] != postorder[pos];i--,size++)
- ;
- node.left = helper(inorder,start,end-size-1,postorder,pos-size-1);
- node.right = helper(inorder,end-size+1,end,postorder,pos-1);
- return node;
- }
- }
leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal----- java的更多相关文章
- 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: ...
- (二叉树 递归) 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 ...
随机推荐
- Python中通过多个字符分割(split)字符串的方法
python中字符串自带的split方法一次只能使用一个字符对字符串进行分割,但是python的正则模块则可以实现多个字符分割 import re re.split('-|_','sharejs_ha ...
- C++-标准输入输出
1,cout 1) 用来向标准输出打印. 2) 如果参数是char*类型,则直接输出字符串.如果想要输出地址,则需要强制转换: <<static_cast<void*>(con ...
- Problem B 队列
Description Two bored soldiers are playing card war. Their card deck consists of exactly n cards, nu ...
- 如何获取google可以访问的IP地址
由于某些原因,google的部分网站无法打开,导致我们的好些资源都无法找到,今天在网上看到一篇文件,教大家如何能找到可以访问的google. 假如我们需要访问的是:https://code.googl ...
- Browser GetImage
using Microsoft.Win32; using System; using System.ComponentModel; using System.Drawing; using System ...
- ios上架
1.登录developer.apple.com 2.点击member center后 进下图 3.点击certificates Identifiers进下图 4.点击Certificates进下图,首 ...
- FastReport产品介绍及免费下载地址
公司地址: 俄罗斯 公司网址: http://www.fast-report.com 详细信息: 由技术总监Alexander Tzyganenko创建于1998年,Fast Reports, Inc ...
- 极客DIY:使用树莓派制作一架四轴无人机
如果你想DIY一台属于自己的无人机,那么接下来可以阅读这篇文章,阅读完毕之后也许对你会有启发. 这个项目主要用到的零件主要来自Erle Robotics(一个使用Linux系统的开源四轴飞行器项目). ...
- @font-face usage
If you haven’t been living in a cave for the past few months, you will have heard lots of talk about ...
- Android.mk中添加宏定义
在Boardconfig.mk 中添加一个 IS_FLAG := true 由于Boardconfig.mk和各目录的Android.mk是相互关联的 所以我们可以在Android.mk 中添加 一个 ...