leetcode106~Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:mac app store更新 编辑:程序博客网 时间:2024/04/29 14:02
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
根据中序遍历和后序遍历创建一棵二叉树。
思路与上题类似。
关键在于,上下限的选取。下面两种方法,不同在于上下限选取不一样。
public class BuildTree106 { public TreeNode buildTree(int[] inorder,int[] postorder) { int inLength = inorder.length; int postLength = postorder.length; return helper(inorder,0,inLength-1,postorder,0,postLength-1); } private TreeNode helper(int[] inorder, int instart, int inend, int[] postorder, int poststart, int postend) { if(instart>inend || poststart>postend) { return null; } int rootval = postorder[postend]; int rootIndex = 0; for(int i=0;i<=inend;i++) { if(inorder[i]==rootval) { rootIndex = i; break; } } int len = inend-rootIndex; TreeNode root = new TreeNode(rootval); root.left = helper(inorder,instart,rootIndex-1,postorder,poststart,postend-len-1); root.right = helper(inorder,rootIndex+1,inend,postorder,postend-len,postend-1); return root; } private TreeNode helper2(int[] inorder, int instart, int inend, int[] postorder, int poststart, int postend) { if(instart>inend || poststart>postend) { return null; } int rootval = postorder[postend]; int rootIndex = 0; for(int i=0;i<=inend;i++) { if(inorder[i]==rootval) { rootIndex = i; break; } } int len = rootIndex-instart; TreeNode root = new TreeNode(rootval); root.left = helper(inorder,instart,rootIndex-1,postorder,poststart,poststart+len-1); root.right = helper(inorder,rootIndex+1,inend,postorder,poststart+len,postend-1); return root; }
0 0
- [JAVA]LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode106—Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode106:Construct Binary Tree from Inorder and Postorder Traversal
- leetcode106~Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal
- leetcode106. Construct Binary Tree from Inorder and Postorder Traversal
- *(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
- 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
- Batch Normalization解析(Caffe版)
- Android之实现视频播放
- Java抽象类的作用和使用
- android 6.0以上版本拍照功能实现
- 本地图片选择
- leetcode106~Construct Binary Tree from Inorder and Postorder Traversal
- 家庭的用电都是交流电,为何会有零线和火线的区别?
- 详解Hadoop核心架构HDFS+MapReduce+Hbase+Hive
- 第14章 线程机制:并发函数的使用--多线程Web服务器
- 用户画像分析
- Go语言TCP Socket编程
- JDBC(一) 简介与获取数据连接
- iOS的截屏功能实现
- Java访问修饰符