【LeetCode-106】Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:烟台淘宝运营 编辑:程序博客网 时间:2024/05/18 21:07
和LeetCode-105是相同的原理,这次只需要将后续遍历序列从后向前遍历,这道题的关键是递归时参数的设定要想清楚
public class ConstructBinaryTreefromInorderandPostorderTraversa {public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}}public TreeNode buildTree(int[] inorder, int[] postorder) {return assistBuildTree(inorder,0,inorder.length - 1,postorder,0,postorder.length - 1); }public TreeNode assistBuildTree(int[] inorder,int ibegin,int iend,int[] postorder,int pbegin,int pend){if (pbegin > pend) {return null;}int povit = postorder[pend];//在中序遍历中找到povit的位置int i = ibegin;for(;i <= iend;i ++){if(inorder[i] == povit){break;}}int rightLength = iend - i;TreeNode root = new TreeNode(povit);//这个参数必须理清楚,理清楚!root.right = assistBuildTree(inorder, i + 1, iend, postorder, pend - rightLength, pend - 1);root.left = assistBuildTree(inorder, ibegin, i - 1, postorder, pbegin, pend - rightLength - 1);return root;}}
0 0
- LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode(106)Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal [106]
- 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
- Construct Binary Tree from Inorder and Postorder Traversal - LeetCode 106
- LeetCode 106:Construct Binary Tree from Postorder and Inorder 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
- 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
- ClassTemplateLoader模板使用学习
- Python 日期处理
- poj--1141 Brackets Sequence(dp)
- 实习入职第二天:ant学习(1)
- php创建软链接
- 【LeetCode-106】Construct Binary Tree from Inorder and Postorder Traversal
- 程序员这个职业的危险期你知道吗
- MVC5中后台提供Json,前台处理Json,绑定给Dropdownlist的例子
- cocopods升级到1.0,导入第三方库,出现问题及解决方式
- 源码提供!Android即时通讯和sns开源项目汇总
- CocoaPods的最新安装流程
- BZOJ 4521: [Cqoi2016]手机号码
- caffe学习笔记(1):数据层定义
- UITableView 自定义cell