|
| 1 | +package com.diguage.algorithm.leetcode; |
| 2 | + |
| 3 | +import com.diguage.algorithm.util.JsonUtils; |
| 4 | +import com.diguage.algorithm.util.TreeNode; |
| 5 | + |
| 6 | +/** |
| 7 | + * = 105. Construct Binary Tree from Preorder and Inorder Traversal |
| 8 | + * |
| 9 | + * https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/[Construct Binary Tree from Preorder and Inorder Traversal - LeetCode] |
| 10 | + * |
| 11 | + * Given preorder and inorder traversal of a tree, construct the binary tree. |
| 12 | + * |
| 13 | + * *Note:* |
| 14 | + * |
| 15 | + * You may assume that duplicates do not exist in the tree. |
| 16 | + * |
| 17 | + * For example, given |
| 18 | + * |
| 19 | + * ---- |
| 20 | + * preorder = [3,9,20,15,7] |
| 21 | + * inorder = [9,3,15,20,7] |
| 22 | + * ---- |
| 23 | + * |
| 24 | + * Return the following binary tree: |
| 25 | + * |
| 26 | + * ---- |
| 27 | + * 3 |
| 28 | + * / \ |
| 29 | + * 9 20 |
| 30 | + * / \ |
| 31 | + * 15 7 |
| 32 | + * ---- |
| 33 | + * |
| 34 | + * @author D瓜哥, https://www.diguage.com/ |
| 35 | + * @since 2020-01-04 20:25 |
| 36 | + */ |
| 37 | +public class ConstructBinaryTreeFromPreorderAndInorderTraversal { |
| 38 | + /** |
| 39 | + * Runtime: 10 ms, faster than 34.14% of Java online submissions for Construct Binary Tree from Preorder and Inorder Traversal. |
| 40 | + * |
| 41 | + * Memory Usage: 36.1 MB, less than 100.00% of Java online submissions for Construct Binary Tree from Preorder and Inorder Traversal. |
| 42 | + * |
| 43 | + * Copy from: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/34538/My-Accepted-Java-Solution[My Accepted Java Solution - LeetCode Discuss] |
| 44 | + */ |
| 45 | + public TreeNode buildTree(int[] preorder, int[] inorder) { |
| 46 | + return build(preorder, inorder, 0, 0, inorder.length); |
| 47 | + } |
| 48 | + |
| 49 | + private TreeNode build(int[] preorder, int[] inorder, int preStart, int inStart, int inEnd) { |
| 50 | + if (preStart > preorder.length - 1 || inStart > inEnd) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + int value = preorder[preStart]; |
| 54 | + int index = inStart; |
| 55 | + for (int i = inStart; i <= inEnd; i++) { |
| 56 | + if (inorder[i] == value) { |
| 57 | + index = i; |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + TreeNode root = new TreeNode(value); |
| 62 | + root.left = build(preorder, inorder, preStart + 1, inStart, index - 1); |
| 63 | + root.right = build(preorder, inorder, preStart + (index - inStart) + 1, index + 1, inEnd); |
| 64 | + return root; |
| 65 | + } |
| 66 | + |
| 67 | + public static void main(String[] args) { |
| 68 | + ConstructBinaryTreeFromPreorderAndInorderTraversal solution = new ConstructBinaryTreeFromPreorderAndInorderTraversal(); |
| 69 | + int[] preorder = {3, 9, 20, 15, 7}; |
| 70 | + int[] inorder = {9, 3, 15, 20, 7}; |
| 71 | + TreeNode tree = solution.buildTree(preorder, inorder); |
| 72 | + System.out.println(JsonUtils.toJson(tree)); |
| 73 | + } |
| 74 | +} |
0 commit comments