leetcode-106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:好用的编程工具 编辑:程序博客网 时间:2024/06/16 10:17
leetcode-106. Construct Binary Tree from Inorder and Postorder Traversal
题目:
Given inorder and postorder traversal of a tree, construct the binary tree
You may assume that duplicates do not exist in the tree.
这题思路很前一题一模一样,这里不再赘述leetcode-105. Construct Binary Tree from Preorder and Inorder Traversal
/** * 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) { return helper(inorder,0,inorder.length-1,postorder,0,postorder.length-1); } private TreeNode helper(int[] inorder, int il, int ir, int[] postorder, int pl, int pr){ if(il>ir) return null; if(il==ir)return new TreeNode(inorder[ir]); int t = 0; for(int i = il ; i <= ir ; i++){ if(inorder[i]==postorder[pr]){ t = i; break; } } // System.out.println(il+" "+ir+" "+pl+" "+pr+" "+t); TreeNode ret = new TreeNode(postorder[pr]); ret.left = helper(inorder,il,t-1,postorder,pl,pr+t-ir-1); ret.right = helper(inorder,t+1,ir,postorder,pr+t-ir,pr-1); return ret; }}
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
- 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
- [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
- Java数据结构和算法:线性表
- 产品设计-电商中商品体系
- arcmap 简化线 简化面 没法填写简化容差值 解决方案
- BEMSimpleLineGraph 优雅折线图框架简单使用
- Linux下获取程序绝对路径(全路径)的方法
- leetcode-106. Construct Binary Tree from Inorder and Postorder Traversal
- iOS 开发 新版支付宝支付集成开发详解和遇到的坑 2017-01-04
- Android学习Material design中的底部导航栏BottomNavigationBar
- Android 事件拦截和分发机制分析
- java.math.Math类常用的常量和方法
- SSD Faster-RCNN使用自己的数据fine-tune训练模型
- 2017年英国A-Level考试时间汇总
- Item 29:追求异常安全的代码
- mysql 常用函数