[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:人工智能出版文献 编辑:程序博客网 时间:2024/05/29 04:55
题目描述
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
解题思路
乍一看题,好似无从解决,但画一个二叉树,并写出它的中序遍历和后序遍历结果,即可发现规律。
上图中可以发现后序遍历的数组的末尾元素即为根节点,根据根节点数值可以确定中序遍历数组中根节点的位置pos,继而可以得到:
- 左子树的中序遍历数组长度为pos-1,元素为inorder[0]~inorder[pos-1];
- 左子树的后序遍历数组长度为pos-1,元素为postorder[0]~postorder[pos-1];
- 右子树的中序遍历数组长度为inorder.length-1-pos,元素为inorder[pos+1]~inorder[inorder.length-1];
- 右子树的中序遍历数组长度为inorder.length-1-pos,元素为postorder[pos]~postorder[postorder.length-2]。
通过规律,我们很容易得到本题的递归解法。
至于本题的非递归解法还在研究中,容以后补上;
代码
public TreeNode buildTree(int[] inorder, int[] postorder) { TreeNode root; int pos = 0; if(inorder==null || postorder==null){ return null; } if(inorder.length==0|| postorder.length==0){ return null; } root = new TreeNode(postorder[postorder.length - 1]); for(int i = 0;i < inorder.length;i++){ if(inorder[i]==postorder[postorder.length - 1]){ pos = i; break; } } int[] inorderLeftChild = Arrays.copyOfRange(inorder, 0, pos); int[] postorderLeftChild = Arrays.copyOfRange(postorder, 0, pos); int[] inorderRightChild = Arrays.copyOfRange(inorder, pos+1, inorder.length); int[] postorderRightChild = Arrays.copyOfRange(postorder, pos, postorder.length-1); TreeNode leftChild = buildTree(inorderLeftChild,postorderLeftChild); TreeNode rightChild = buildTree(inorderRightChild,postorderRightChild); root.left = leftChild; root.right = rightChild; 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
- [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
- 后台利用JS刷新页面(网站)
- C - I Love This Game
- 出现乱码的原因
- Leetcode中的组合排列问题:Permutations,Combinations,Letter Combinations of a Phone Number
- 数组指针和指针数组的区别
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- 第三方
- ExpandableListView 好有列表的布局
- 有关中断的使用
- Linux下实现秒级定时任务的两种方案(crontab 每秒运行)
- glance api服务的镜像缓存机制
- Javscript删除数组中指定元素
- MTK android WIFI地址 不写随机变动问题
- Hibernate主键生成策略总结