Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序重建二叉树@LeetCode
来源:互联网 发布:windows 路由表 命令 编辑:程序博客网 时间:2024/05/16 05:35
同上一题,这一题是由inorder和postorder来确定树,对于postorder,root要从尾部开始找
另外,在从一个order到另外一个order时,要用相对距离来计算!即从第一个order算出dist是多少,然后应用这个dist到第二个order上
package Level4;import Utility.TreeNode;/** * Construct Binary Tree from Inorder and Postorder Traversal * * Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree. * */public class S141 {public static void main(String[] args) {int[] inorder = {1,2,3};int[] postorder = {3,2,1};TreeNode root = buildTree(inorder, postorder);root.print();}public static TreeNode buildTree(int[] inorder, int[] postorder) {if(inorder.length == 0){return null;} return rec(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1); }public static TreeNode rec(int[] inorder, int[] postorder, int inStart, int inEnd, int postStart, int postEnd){if(postEnd < 0 || postEnd>=postorder.length){return null;}TreeNode root = new TreeNode(postorder[postEnd]);int rootIndex;// rootIndex in inorder[]for(rootIndex=0; rootIndex<inorder.length; rootIndex++) {if(inorder[rootIndex] == postorder[postEnd]){break;}}int leftSubTreeLen = rootIndex - inStart;// inorder中,inStart到rootIndex的距离if(rootIndex > inStart){// 锁定范围,否则会Memory out of limit!root.left = rec(inorder, postorder, inStart, rootIndex-1, postStart, postStart+leftSubTreeLen-1);}if(rootIndex < inEnd){root.right = rec(inorder, postorder, rootIndex+1, inEnd, postStart+leftSubTreeLen, postEnd-1);}return root;}}
/** * Definition for binary tree * 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 rec(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1); } public TreeNode rec(int[] inorder, int[] postorder, int inorderLeft, int inorderRight, int postorderLeft, int postorderRight) { if(inorderLeft > inorderRight || postorderLeft > postorderRight) { return null; } int root = postorder[postorderRight]; TreeNode rootNode = new TreeNode(root); int inorderRootPos = inorderLeft; for(inorderRootPos=inorderLeft; inorderRootPos<=inorderRight; inorderRootPos++) { if(inorder[inorderRootPos] == root) { break; } } int dist = inorderRootPos - inorderLeft; rootNode.left = rec(inorder, postorder, inorderLeft, inorderRootPos-1, postorderLeft, postorderLeft+dist-1); rootNode.right = rec(inorder, postorder, inorderRootPos+1, inorderRight, postorderLeft+dist, postorderRight-1); return rootNode; }}
- 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
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- LeetCode OJ 之 Construct Binary Tree from Inorder and Postorder Traversal (由二叉树的中序和后序序列构造二叉树)
- 【LeetCode】Construct Binary Tree From Inorder And Postorder Traversal 中序遍历和后序遍历树构造二叉树
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树
- 106.leetcode.Construct Binary Tree from Inorder and Postorder Traversal(medium)[中序、后序构造二叉树]
- 【LeetCode笔记】Construct Binary Tree from Inorder and Postorder Traversal 中序、后序遍历求二叉树
- LeetCode(Construct Binary Tree from Inorder and Postorder Traversal)根据二叉树的中序和后续构造二叉树
- [LintCode] 中序遍历和后序遍历树构造二叉树 Construct Binary Tree from Inorder and Postorder Traversal
- lintcode construct-binary-tree-from-inorder-and-postorder-traversal 中序遍历和后序遍历树构造二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 根据中序和后序序列生成二叉树(重重)
- Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)
- Construct Binary Tree from Preorder and Inorder Traversal 前序和中序重建二叉树
- Construct Binary Tree from Preorder and Inorder Traversal 从先序和中序重建二叉树@LeetCode
- leetCode 106.Construct Binary Tree from Inorder and Postorder Traversal (根据中序遍历和后序遍历构造二叉树)
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构造二叉树)
- CSDN博客排版的那些事
- get_cfg_var()和ini_get()区别?
- Web Services--gSOAP 2.7.6 User Guide 目录
- 简单的CLI(command line interface)
- C++求点和平面的关系
- Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序重建二叉树@LeetCode
- 机房收费系统——计算消费金额
- Phone List 字典序
- 开通了
- 知识积累
- mysql 绿色版的使用配置
- Apache 的代理使用 配置
- c++笔记
- Creating a View Class//建立视图类