|
| 1 | +package com.brianway.learning.algorithms.leetcode.medium; |
| 2 | + |
| 3 | +import com.brianway.learning.algorithms.leetcode.common.TreeNode; |
| 4 | + |
| 5 | +/** |
| 6 | + * 106. Construct Binary Tree from Inorder and Postorder Traversal |
| 7 | + * Question: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ |
| 8 | + * 关键题设:inorder and postorder consist of unique values. |
| 9 | + * |
| 10 | + * @auther brian |
| 11 | + * @since 2022/9/7 21:08 |
| 12 | + */ |
| 13 | +public class ConstructBinaryTreeFromInorderAndPostorderTraversal { |
| 14 | + public TreeNode buildTree(int[] inorder, int[] postorder) { |
| 15 | + return null; |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * 递归 |
| 20 | + */ |
| 21 | + public class ConstructBinaryTreeFromInorderAndPostorderTraversal0 extends ConstructBinaryTreeFromInorderAndPostorderTraversal { |
| 22 | + @Override |
| 23 | + public TreeNode buildTree(int[] inorder, int[] postorder) { |
| 24 | + return buildTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * root=postorder[postHigh], 在inorder中找到inRootIndex, 则 |
| 29 | + * inorder左子树:[inLow, inRootIndex-1], |
| 30 | + * inorder右子树: [inRootIndex+1, inHigh] |
| 31 | + * <p> |
| 32 | + * 下面确定postorder的左右子树的数组下标边界 |
| 33 | + * 左子树的size=inRootIndex-inLow |
| 34 | + * 右子树的size=inHigh-(inRootIndex+1)+1=inHigh-inRootIndex |
| 35 | + * <p> |
| 36 | + * postorder左子树:[postLow, postLow+左子树的size-1] |
| 37 | + * postorder右子树:[postHigh-1-右子树的size+1 ,postHigh-1] |
| 38 | + * <p> |
| 39 | + * 暂留: |
| 40 | + * 又(postLow+左子树的size-1) +1= postHigh-1-右子树的size+1 |
| 41 | + * postLow+左子树的size= postHigh-右子树的size |
| 42 | + * postLow+inRootIndex-inLow=postHigh-(inHigh-inRootIndex) |
| 43 | + * postLow-inLow=postHigh-inHigh |
| 44 | + * <p> |
| 45 | + * <p> |
| 46 | + * 时间复杂度 O(n log n) 假设二叉树层数为k, 每一层找inorder的根节点下标都平均需要遍历 n/2, 所以 时间复杂度 O(k*n), |
| 47 | + * 极端情况下,k=n,且每一层遍历都是n, 时间复杂度退化为 O(n^2) |
| 48 | + * 空间复杂度 O(1) |
| 49 | + */ |
| 50 | + public TreeNode buildTree(int[] inorder, int inLow, int inHigh, int[] postorder, int postLow, int postHigh) { |
| 51 | + // 终止条件 |
| 52 | + if (inHigh < inLow) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + if (inHigh == inLow) { |
| 56 | + return new TreeNode(inorder[inHigh]); |
| 57 | + } |
| 58 | + |
| 59 | + int root = postorder[postHigh]; |
| 60 | + // find inRootIndex |
| 61 | + int inRootIndex = findInorderRootIndex(root, inorder, inLow, inHigh); |
| 62 | + TreeNode left = buildTree(inorder, inLow, inRootIndex - 1, |
| 63 | + postorder, postLow, postLow + inRootIndex - inLow - 1); |
| 64 | + TreeNode right = buildTree(inorder, inRootIndex + 1, inHigh, |
| 65 | + postorder, postHigh - (inHigh - inRootIndex), postHigh - 1); |
| 66 | + return new TreeNode(root, left, right); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public int findInorderRootIndex(int root, int[] inorder, int low, int high) { |
| 71 | + for (int i = low; i <= high; i++) { |
| 72 | + if (inorder[i] == root) { |
| 73 | + return i; |
| 74 | + } |
| 75 | + } |
| 76 | + throw new IllegalArgumentException("not found"); |
| 77 | + } |
| 78 | +} |
0 commit comments