Java input a binary tree in the first sequence traversal and the results of the middle sequence traversal, please rebuild the two-fork tree. __ algorithm

Source: Internet
Author: User


Enter the sequence traversal of a binary tree and the results of the middle sequence traversal, please reconstruct the two-fork tree. Assuming that the input sequence and the results of the sequence sequences do not contain duplicate numbers, such as the input sequence {1,2,4,7,3,5,6,8} and the sequence {4,7,2,1,5,3,8,6}, the tree is rebuilt and its head node is exported, and the header node of the binary tree is defined as follows:

struct binarytreenode{

int m_value;

Binrytreenode *pleft;

Binarytreenode *pright;

}

In fact, the implementation of the various traversal algorithms for binary tree is much more difficult than understanding, for me in the data structure class, on the front, the middle, the last three traversal thought has a sense of understanding (that is, give me a tree, I can definitely write out its three kinds of traversal sequence, It can also be done by the known pre (back) sequence and the middle sequence sequence, but the actual code implementation is more stupid ... It's hard to be accurate in the way that recursion needs to run N times like a computer in your head ... Each encounter recursive algorithm is very awkward, see understand but not write their own, resulting in their dynamic programming algorithm to learn a rotten 0.0, so for the return to strengthen the study ah ...

If you understand the recursive access, then the process of achievement is much easier, the first number of sequential traversal sequences (the last number of subsequent traversal) must be the root node, so we can divide the sequence into two parts of Saozi right according to the position of the node in the sequence. The same rule can be used to determine each intermediate node in the Saozi right child number.


public class Solution {public
    TreeNode reconstructbinarytree (int [] pre,int [] in) {
      TreeNode root=new TreeNode (p RE[0]);//The first number of the preceding order is defined as the root
	int len=pre.length;
	When only a number of times
	if (len==1) {
        root.left=null;
        Root.right=null;
		return root;
	}
	Locate the root position int rootval=root.val in the middle order
	;
	int i;
	for (i=0;i<len;i++) {
		if (Rootval==in[i]) break
			;
	Create a left subtree
	if (i>0) {
		int[] pr=new int[i];
		Int[] Ino=new int[i];
		for (int j=0;j<i;j++) {
			pr[j]=pre[j+1];
		}
		for (int j=0;j<i;j++) {
			ino[j]=in[j];
		}
		Root.left=reconstructbinarytree (Pr,ino);
	} else{
		root.left=null;
	}
	Create right subtree
	if (len-i-1>0) {
		int[] pr=new int[len-i-1];
		Int[] Ino=new int[len-i-1];
		for (int j=i+1;j<len;j++) {
			ino[j-i-1]=in[j];
			PR[J-I-1]=PRE[J];
		}
		Root.right=reconstructbinarytree (Pr,ino);
	} else{
		root.right=null;
	}
	
	
    return root;
    }
}

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.