-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ConstructBinaryTreefromPreorderandInorderTraversal.java
- Loading branch information
1 parent
ec00341
commit c5c04e8
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/ConstructBinaryTreefromPreorderandInorderTraversal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package Tree; | ||
/** | ||
* | ||
* @author chenqun | ||
* Given preorder and inorder traversal of a tree, construct the binary tree. | ||
* | ||
*/ | ||
public class ConstructBinaryTreefromPreorderandInorderTraversal { | ||
|
||
public TreeNode buildTree(int[] preorder, int[] inorder){ | ||
if(preorder == null && inorder == null || preorder.length == 0) return null; | ||
TreeNode root = new TreeNode(preorder[0]); | ||
int i=0; | ||
for(i=0;i<inorder.length;i++){ | ||
if(inorder[i] == preorder[0]) | ||
break; | ||
} | ||
//ÒÔiΪ·Ö½çµã | ||
int[] new_inorder_left,new_inorder_right,new_preorder_left,new_preorder_right; | ||
if(i < inorder.length){ | ||
new_inorder_left = new int[i]; | ||
System.arraycopy(inorder, 0, new_inorder_left, 0, i); | ||
new_preorder_left = new int[i]; | ||
System.arraycopy(preorder, 1, new_preorder_left, 0, i); | ||
root.left = buildTree(new_preorder_left,new_inorder_left); | ||
new_inorder_right = new int[inorder.length-1-i]; | ||
System.arraycopy(inorder, 1+i, new_inorder_right, 0, inorder.length-1-i); | ||
new_preorder_right = new int[preorder.length-1-i]; | ||
System.arraycopy(preorder, 1+i, new_preorder_right, 0, preorder.length-1-i); | ||
root.right = buildTree(new_preorder_right,new_inorder_right); | ||
} | ||
return root; | ||
} | ||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
} |