Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:北津学院教务网络管 编辑:程序博客网 时间:2024/06/05 12:00
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 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) { if(postorder == null || inorder == null){ return null; } HashMap<Integer,Integer> map = new HashMap<>(); for(int i = 0; i < inorder.length;i++){ map.put(inorder[i],i); } return recursion(postorder,inorder,0, postorder.length-1,0,inorder.length-1,map); } public TreeNode recursion(int[] postorder, int[] inorder, int postLeft, int postRight, int inLeft, int inRight, HashMap<Integer, Integer> map){ if(postLeft > postRight || inLeft > inRight){ return null; } TreeNode root = new TreeNode(postorder[postRight]); int index = map.get(root.val); root.left = recursion(postorder,inorder,postLeft, index-inLeft+postLeft-1,inLeft,index-1,map); root.right = recursion(postorder,inorder,postRight-(inRight-index),postRight-1,index+1,inRight,map); return root; }}
阅读全文
0 0
- *(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
- leetcode 110: 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
- 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
- 机器学习中的SVD
- 实现MFC的两种类型文件对话框
- Spring Cloud学习
- Java Future/Callable基础应用
- 防盗链概念详解以及自己使用tomcat实现一个防盗链
- Construct Binary Tree from Inorder and Postorder Traversal
- 用三分法解决问题 ycb老师与一道简单的物理题
- Spring 4 官方文档学习(⑤)WebSocket支持
- HDU1151 (10/600)
- extjs 布局 -列布局 column
- 添加基本组件
- 在pycharm2017 上实现python3.6与mysql数据库的连接
- javascript 原型方法,对象方法,类方法;私有属性,公有属性,公有静态属性
- Kotlin基本语法