diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 5ae62f9..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index b26911b..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml deleted file mode 100644 index d411041..0000000 --- a/.idea/libraries/Maven__junit_junit_4_12.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml deleted file mode 100644 index f58bbc1..0000000 --- a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index a0b0171..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 8fd8bd1..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index eddc618..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,1202 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - true - DEFINITION_ORDER - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - project - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1477058380702 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - No facets are configured - - - - - - - - - - - - - - - 1.8 - - - - - - - - LeetCode - - - - - - - - Maven: junit:junit:4.12 - - - - - - - - \ No newline at end of file diff --git a/src/main/java/binarytree/TreeNode.java b/src/main/java/binarytree/TreeNode.java index 12613b1..bcefd78 100644 --- a/src/main/java/binarytree/TreeNode.java +++ b/src/main/java/binarytree/TreeNode.java @@ -1,11 +1,9 @@ package binarytree; import sun.reflect.generics.tree.Tree; +import utils.ArrayUtils; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.Queue; -import java.util.Stack; +import java.util.*; /** * Created by tianliangxia on 16-10-22. @@ -227,8 +225,49 @@ public static boolean match(TreeNode root, TreeNode pattern){ || match(root.left, pattern) || match(root.right, pattern); } + /*** + * 由前序中序得到TreeNode + * 数字不能重复 + * @param pre + * @param in + * @return + */ + public static TreeNode constructWithPreAndInOrder(int[] pre, int[] in){ + if(pre == null || pre.length == 0 || in == null || in.length == 0) + return null; + int root_val = pre[0]; + int idx = ArrayUtils.indexOf(in, root_val); + int left_len = idx; + int right_len = pre.length-left_len-1; + TreeNode root = new TreeNode(root_val); + int[] pre_left = Arrays.copyOfRange(pre, 1, 1+left_len); + int[] pre_right = Arrays.copyOfRange(pre, idx+1, idx+1+right_len); + int[] in_left = Arrays.copyOfRange(in, 0, left_len); + int[] in_right = Arrays.copyOfRange(in, idx+1, idx+1+right_len); + root.left = constructWithPreAndInOrder(pre_left, in_left); + root.right = constructWithPreAndInOrder(pre_right, in_right); + return root; + } + + public static TreeNode constructWithInAndPostOrder(int[] in, int[] post){ + if(post == null || post.length == 0 || in == null || in.length == 0) + return null; + int root_val = post[post.length-1]; + int idx = ArrayUtils.indexOf(in, root_val); + int left_len = idx; + int right_len = post.length-left_len-1; + TreeNode root = new TreeNode(root_val); + int[] post_left = Arrays.copyOfRange(post, 0, left_len); + int[] post_right = Arrays.copyOfRange(post, left_len, post.length-1); + int[] in_left = Arrays.copyOfRange(in, 0, left_len); + int[] in_right = Arrays.copyOfRange(in, idx+1, idx+1+right_len); + + root.left = constructWithInAndPostOrder(in_left, post_left); + root.right = constructWithInAndPostOrder(in_right, post_right); + return root; + } } diff --git a/src/main/java/utils/ArrayUtils.java b/src/main/java/utils/ArrayUtils.java new file mode 100644 index 0000000..f63850c --- /dev/null +++ b/src/main/java/utils/ArrayUtils.java @@ -0,0 +1,21 @@ +package utils; + +/** + * Created by tianliangxia on 16-10-22. + */ +public class ArrayUtils { + public static int indexOf(int[] array, int beg, int end, int key){ + if(beg>end){ + throw new IndexOutOfBoundsException(); + } + for(int i=beg;i