Construct Binary Tree from preorder and inorder traversal

Source: Internet
Author: User

First, the topic

1, examining

2. Analysis

In order to find the original structure of the two-fork tree, a sequence of the first order and middle sequence traversal of the two-fork tree is given.

Second, the answer

1, Ideas:

The characteristics of first order and middle sequence traversal of binary tree are as follows:

①, First Order (preorder): root-to-left-to-right;

②, middle order (inorder): left-to-right;

The first node of the ③, first order is the root node of the whole binary tree (root), and the root node divides the sequence sequences into two parts, the root left is the root of all the children of the left subtree, root to the right of the root right of all the child nodes of the subtree;

The order of the ④ and ordinal nodes is exactly ordered in the array, the order of the nodes in the sequence, if Index (NODE1) < index (NODE2), Node2 is the ancestor node of Node1, or the right child node of the ancestor node, or the right child of Node1 or its descendants node;

Method One,

Recursive method is used.

①, preorder the first node as root, then root appears in the inorder subscript is index,

Then the preorder (1,index) node is the order of first order traversal of the left subtree of root, inorder (0, index-1) is the order of the middle order traversal of the left subtree of root,

In the same vein, preorder (index +1, len-1) is the first order traversal sequence of the right subtree of root, and inorder (index+1,len-1) is the middle sequence of the right subtree of root.

②, recursive jump condition is, root in preorder subscript > preorder Array maximum subscript, or inorder left > right.

 PublicTreeNode Buildtree (int[] Preorder,int[] inorder) {Map<integer, integer> map =NewHashmap<integer, integer>();  for(inti = 0; i < inorder.length; i++) Map.put (Inorder[i], i); returnHelper (0, 0, inorder.length-1, preorder, inorder, map); }        PrivateTreeNode Helper (intPrestart,intInstart,intInend,int[] Preorder,int[] inorder, Map<integer, integer>map) {                //jump out of recursion condition        if(Prestart > Preorder.length-1 | | instart >inend)return NULL; TreeNode Root=NewTreeNode (Preorder[prestart]); intInindex =Map.get (Root.val); Root.left= Helper (prestart+1, Instart, inIndex-1, preorder, inorder, map); Root.right= Helper (Prestart + (Inindex-instart + 1), Inindex + 1, Inend, preorder, inorder, map); returnRoot; }

  

Method Two,

Create a stack, in sequence traversal order into the stack, into the stack, to determine the node in the middle sequence traversal and the location of the top element of the relationship:

①, Jozheng sequence traversal in the top of the stack element in front of this node, then this node must be the top node of the left child.

②, Jozheng sequence traversal in the station top element behind this node, then the top of the station stack, until the stack is empty, or the station top element in the middle sequence traversal of the position of the value of the node behind;

At this point, this node must be the right child of the top element of the front.

 PublicTreeNode Buildtree (int[] Preorder,int[] inorder) {       if(Preorder.length = = 0)            return NULL; Map<integer, integer> map =NewHashmap<integer, integer>();  for(inti = 0; i < inorder.length; i++) Map.put (Inorder[i], i); Stack<TreeNode> stack =NewStack<>(); intValue = preorder[0]; TreeNode Root=NewTreeNode (value);                Stack.push (root);  for(inti = 1; i < preorder.length; i++) {Value=Preorder[i]; TreeNode node=NewTreeNode (value); if(Map.get (value) <Map.get (Stack.peek (). val)) {Stack.peek (). Left=node; }            Else{TreeNode Parent=NULL;  while(!stack.isempty () && map.get (value) >Map.get (Stack.peek (). val)) Parent= Stack.pop ();//stack is empty when jumping out of the loop (i.e. node is the root node right child),//or stack stack top element in inorder order greater than node jump loop (that is, node is the top of the right child)Parent.right =node;        } stack.push (node); }        returnRoot; }

      

Construct Binary Tree from preorder and inorder traversal

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.