diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode106.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode106.java new file mode 100644 index 0000000..55d4f76 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode106.java @@ -0,0 +1,55 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/25 21:29 + * @注释 + */ +public class LeetCode106 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{3,9,20,null,null,15,7}); + // 9 3 20 7 15 + // 20 3 7 9 15 + // 20 7 3 15 9 + new Solution().buildTree(new int[]{9,3,15,20,7}, new int[]{9,15,7,20,3}).printTree(); + + } + + class Solution { + public TreeNode buildTree(int[] inorder, int[] postorder) { + if (inorder.length == 0) { + return null; + } + return circle(inorder, postorder, 0, inorder.length - 1, postorder.length - 1); + + } + public TreeNode circle( + int[] inorder, + int[] postorder, + int inorderStart, + int inorderEnd, + int poster + ) { + if (poster < 0 || inorderStart > inorderEnd) { + return null; + } + int lastValue = postorder[poster]; + // 中序的指针,左边为左树,右边为右树。 + int index = 0; + while (inorder[index] != lastValue) { + index++; + } + TreeNode root = new TreeNode(lastValue); + root.right = circle(inorder, postorder, index + 1, inorderEnd, index+inorderEnd-2); + root.left = circle(inorder, postorder, inorderStart, index - 1,inorderStart+index-1 ); + return root; + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java index a8be111..116f0a5 100644 --- a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java +++ b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java @@ -160,6 +160,7 @@ public class TreeNode { while (index < length) { index++; if (index >= length) { + root.printTree(); return root; } currNode = nodeQueue.poll(); @@ -170,6 +171,7 @@ public class TreeNode { } index++; if (index >= length) { + root.printTree(); return root; } Integer rightChild = array[index]; @@ -178,7 +180,7 @@ public class TreeNode { nodeQueue.offer(currNode.right); } } - + root.printTree(); return root; }