Construct Binary Tree from preorder and inorder traversal

Source: Internet
Author: User

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

Given Preorder and inorder traversal of a tree, construct the binary tree.

Note:
Assume that duplicates does not exist in the tree.

Problem Solving Ideas:

This is a difficult topic in two-fork tree traversal, but it is also more common, with a deeper understanding of pre, in, or post traversal.

Http://articles.leetcode.com/2011/04/construct-binary-tree-from-inorder-and-preorder-postorder-traversal.html

The main idea is to refer to the above article.

For the following two-fork tree:

7

/    \

10 2

/     \      /

4 3 8

\     /

1 11

Preorder Traversal results: 7 10 4 3 1 2 8 11

Inorder Convenience Results: 4 10 3 1 7 11 8 2

To solve this problem, we should use the two properties of preorder and inorder traversal respectively. The first element of the Preoder is determined as the root node, and then in Inorder, the left side of it is all nodes of the Zuozi, and the right is all the nodes of the right subtree. And then to the Zuozi these nodes, recursively call the above method.

For example, above, 7 is the first element of preorder, so it is the root of the whole tree. Then in the Inorder, 7 to the left 4-1 is the left subtree, to the right 11-2 is the right sub-tree. We go back to preorder, 10 is the root of Zuozi, and 2 is the root of the right subtree. Again to preorder inside, 10 left of 4 is left dial hand tree, right of 3, 1 is right sub-tree. 2 to the left of 11 and 8 for the left dial hand tree. Recursively, until the end.

So the recursion here is actually the left and right boundary of the two arrays, preorder and inorder. Root is always the first element within the preorder boundary. Then use the root index in the inorder to update the scope of the next recursive inorder.

How is the scope of the preorder determined? From the root to calculate the length, the left subtree length is the left dial hand tree range, the right subtree length, is the right sub-tree range.

The code is as follows:

/*** Definition for Binary tree * public class TreeNode {* Int. val; * TreeNode left; * TreeNode right; * TreeNode (int x) {val = x;} }*/ Public classSolution { PublicTreeNode Buildtree (int[] Preorder,int[] inorder) {        returnHelper (preorder, inorder, 0, preorder.length-1, 0, Inorder.length-1); }         PublicTreeNode Helper (int[] Preorder,int[] inorder,intPrestart,intPreend,intInstart,intinend) {        if(Prestart > Preend | | instart >inend) {            return NULL; } TreeNode Node=NewTreeNode (Preorder[prestart]); intindex = 0;  for(inti = Instart; I <= inend; i++){            if(Inorder[i] = =Preorder[prestart]) {Index=i;  Break; }} node.left= Helper (preorder, inorder, Prestart + 1, Prestart + Index-instart, Instart, index-1); Node.right= Helper (preorder, inorder, Prestart + index-instart + 1, preend, index + 1, Inend); returnnode; }}

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.