leetCode-Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:开票软件默认密码 编辑:程序博客网 时间:2024/06/05 03:35
Description:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Solution:
//中序序列和后序序列构造二叉树,与前序序列和中序序列构建二叉树异曲同工,都是利用前序或者后序序列找出根节点(前序序列首端,后序序列末端为根节点),然后利用根节点在中序序列中找出左右子树的个数,然后在分割前序序列或者后序序列,之后递归构建树.如果有不明白的地方可以看看这篇博文:http://blog.csdn.net/zhangjiajie023/article/details/7912757/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { Map<Integer,Integer>map = new HashMap<Integer,Integer>(); for(int i = 0;i < inorder.length;i++){ map.put(inorder[i],i); } return inpoTree(inorder,0,inorder.length - 1,postorder,0,postorder.length - 1,map); } TreeNode inpoTree(int[] inorder,int instart,int inend,int[] postorder,int poststart,int postend,Map<Integer,Integer>map){ if(instart > inend || poststart > postend){ return null; } TreeNode node = new TreeNode(postorder[postend]); int index = map.get(postorder[postend]); int numsleft = index - instart; int numsright = inend - index; node.left = inpoTree(inorder,instart,index - 1,postorder,poststart,poststart + numsleft - 1,map); node.right = inpoTree(inorder,index + 1,inend,postorder,poststart + numsleft,postend - 1,map); return node; }}
阅读全文
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
- [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
- [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
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- unity做可视化音乐播放器_生成图形篇(圆形)
- 先验概率与后验概率、似然函数
- 7-15 PAT Judge
- 台大DL-week2笔记
- 变化检测----多元变化检测(Multivariate Alteration Detection)
- leetCode-Construct Binary Tree from Inorder and Postorder Traversal
- 几种常见DRL(深度强化学习)方法总结与对比之前提基本概念
- 重写和重载的区别
- tensorflow.cast参数及功能
- Fuzzing简介以及使用AFL对LibTIFF进行模糊测试
- rails官方指南--建一个简易博客
- C++操作MySQL
- Java中怎么把科学计数法显示出全部数字
- CTS测试框架 -- V1版本