Sword refers to the Java Implementation of offer programming questions -- interview question 6 re-constructs a binary tree, and sword refers to offer

Source: Internet
Author: User

Sword refers to the Java Implementation of offer programming questions -- interview question 6 re-constructs a binary tree, and sword refers to offer

Question:

Enter the result of the forward and middle traversal of a binary tree. Create a New Binary Tree. Assume that the input results do not contain repeated numbers.For example, if the input preorder traversal sequence {,} and the middle order traversal sequence {,}, then the binary tree is rebuilt and its root node is output.

In the forward traversal of a binary tree, the first number is always the root node of the tree. In the middle-order traversal, the root node of the tree is in the middle of the sequence. The value of the Left subtree is on the left of the root node, and the value of the right subtree is on the right of the root node value.

Therefore, you need to scan the ordinal traversal sequence to find the value of a very node, so that you can find the number of nodes in the left subtree and the number of nodes in the right subtree, then, find the root node of the Left subtree in the previous traversal sequence, and then find the left subtree and the right subtree in the middle traversal sequence. Recursion. Because the construction of Binary Trees is implemented recursively, it is very easy to implement the reconstruction of Binary trees using recursion.

1 package Solution; 2 3/** 4 * sword refers to offer interview question 6: rebuilding a binary tree 5 * question: Enter the results of a binary tree's forward and middle traversal, re-create the binary tree. Assume that the input results do not contain repeated numbers. 6 * For example, input pre-order traversal sequences {,} and Middle-order traversal sequences }, then the reconstructed binary tree is output and its root node is output. 7*1 8 */\ 9*2 310 * // \ 11*4 5 612*\/13*7 814 * @ author GL15 * 16 */17 public class No6ReConstructBinaryTree {18 19 public static void main (String [] args) {20 int [] preOrder = {1, 2, 4, 7, 3, 5, 6, 8}; 21 int [] inOrder = {4, 7, 2, 1, 5, 3, 8, 6 }; 22 BinaryTreeNode node = reConstruct (preOrder, inOrder); 23 printTree (node); 24} 25 // Binary Tree node 26 public static class BinaryTreeNode {27 int value; 28 BinaryTreeNode left; 29 BinaryTreeNode right; 30} 31 32/** 33 * judge the validity of the input 34 * @ param preOrder35 * @ param inOrder36 * @ return37 */38 public static BinaryTreeNode reConstruct (int [] preOrder, int [] inOrder) {39 if (preOrder = null | inOrder = null | preOrder. length! = InOrder. length | preOrder. length <1) 40 return null; 41 return construct (preOrder, 0, preOrder. length-1, inOrder, 0, inOrder. length-1 ); 42} 43 44/** 45 * build a binary tree based on pre-order traversal and mid-order traversal 46 * @ param preOrder pre-order traversal sequence 47 * @ param ps pre-order traversal start position 48 *@ param pe pre-order traversal end position 49 * @ param inOrder in-order traversal sequence 50 * @ param is in-order traversal start position 51 * @ param ie in-order traversal end position 52 * @ return constructed tree the root node 53 */54 public static BinaryTreeNode construct (int [] preOrder, int ps, int p E, int [] inOrder, int is, int ie) {55 // if the start position is greater than the end position, 56 if (ps> pe) 57 return null has been processed on the leaf node; 58 // traverse the first number in the forward order as the current root node 59 int value = preOrder [ps]; 60 // index is the index 61 int index = is; 62 while (index <= ie & inOrder [index]! = Value) {63 index ++; 64} 65 System. out. println ("the current parameter value is-> ps:" + ps + "pe:" + pe + "is:" + is + "ie: "+ ie +" index: "+ index +" rootValue: "+ value ); 66 // if the root node is not found in the whole middle-order traversal, the input data is invalid. 67 if (index> ie) {68 throw new RuntimeException ("invalid input" + index); 69} 70 BinaryTreeNode node = new BinaryTreeNode (); 71 node. value = value; 72 // The number of left subtree of the current node is index-is73 // The Position of the pre-sequential traversal corresponding to the left subtree in preOrder [ps + 1, ps + index-is] 74 // The Position of the central traversal corresponding to the left subtree is in inOrder [I S, index-1] 75 node. left = construct (preOrder, ps + 1, ps + index-is, inOrder, is, index-1 ); 76 // The number of the right subtree of the current node is ie-index77 // The pre-order traversal position of the right subtree in preOrder [ps + index-is + 1, pe] 78 // The center order traversal position of the right subtree is in inOrder [index + 1, ie] 79 node. right = construct (preOrder, ps + index-is + 1, pe, inOrder, index + 1, ie); 80 return node; 81} 82 // recursively print 83 public static void printTree (BinaryTreeNode node) {84 if (node! = Null) {85 printTree (node. left); 86 System. out. print (node. value + ""); 87 printTree (node. right); 88} 89} 90 91}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.