106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:数据比例图 编辑:程序博客网 时间:2024/04/28 17:34
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
后序的末尾元素为根,此元素在中序中的索引为index。
中序下次传入 [start,index-1] 、[index+1,end]
后序下次传入 [start,start+index-start_inorder-1] 、[end-end_inorder+index,end-1]
public TreeNode buildTree(int[] inorder, int[] postorder){int len=inorder.length;if(len==0)return null;return build(0, len-1, 0, len-1, inorder, postorder);}private TreeNode build(int inl,int inr,int pol,int por,int[] inorder,int[] postorder){if(pol==por)return new TreeNode(postorder[pol]);if(pol>por)return null;int num=postorder[por];int index=-1;for(int i=0;i<inorder.length;i++)if(inorder[i]==num){index=i;break;}TreeNode t=new TreeNode(num);t.left=build(inl, index-1, pol, pol+index-inl-1, inorder, postorder);t.right=build(index+1, inr, por-inr+index, por-1, inorder, postorder);return t;}
0 0
- LeetCode 106. 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
- [LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106.Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Map遍历的2种方式
- 《解密中国女CEO董明珠》米晓海
- <Sicily>Pair
- C++中 模板Template的使用
- 22:24 2016/6/9 PHP基础练习题与答案详解
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
- string函数用法详解
- css样式的优先级
- 自定义Toast
- MySQL 事务没有提交导致锁等待
- 迭代器
- 列表生成式
- 线程池