Construct Binary Tree from Inorder and Postorder Traversal --- LeetCode
来源:互联网 发布:linux snmp测试 编辑:程序博客网 时间:2024/06/05 10:10
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 binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode constructBTree(int[] inorder,int istart,int iend,int[] postorder,int pstart,int pend){
int root=-1;
TreeNode node=new TreeNode(postorder[pend]);
for(int i=istart;i<=iend;i++){
if(postorder[pend]==inorder[i]){
root=i;
break;
}
}
if(root>istart){
int len=root-istart;
node.left=constructBTree(inorder,istart,root-1,postorder,pstart,pstart+len-1);
}
if(root<iend){
int len=iend-root;
node.right=constructBTree(inorder,root+1,iend,postorder,pend-len,pend-1);
}
return node;
}
public TreeNode buildTree(int[] inorder, int[] postorder) {
TreeNode root=null;
int len=postorder.length;
if(postorder==null||len==0){
return null;
}
root=constructBTree(inorder,0,len-1,postorder,0,len-1);
return root;
}
}
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode - Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- JAVA之NIO按行读取大文件
- android乱七八糟
- ActionBar的详解
- 集成融云报错duplicate symbol
- [Unity]内购插件Unibill
- Construct Binary Tree from Inorder and Postorder Traversal --- LeetCode
- C程序模板
- Next Permutation
- 第三篇 设计模式--抽象工厂模式
- 关于iPhone文件系统NSFileManager讲解
- Redis 数据类型值 list
- qq空间营销快速排名技巧
- 根据时间搜索数据
- SQL SERVER与oracle中的递归处理