Leetcode---105. Construct Binary Tree from preorder and inorder traversal

Source: Internet
Author: User

Title Link: 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.

The requirement of this problem is to construct the binary tree through the binary tree's pre-sequence traversal and the middle sequence traversal result.

Binary tree pre-sequence traversal, the root node at the front, and then the next is the Zuozi, the right child node, the binary tree sequence traversal, the root node in the middle, the left side is the Zuozi node, right is the right subtree node. Thus, it can be determined that the first node of the pre-sequence traversal is the root node, while the first node in the middle sequence traversal is the same as the root node, and the left side is the left dial hand tree, and the right is the rightmost number. As for the condition of the left and right subtree, it can be generated by recursion.

For example:

二叉树为:        1       /       2   3     / \       4   5   6前序遍历:    1 -> 2 -> 4 -> 5 -> 3 -> 6    |    |---------|    |----| 根节点    左子树       右子数中序遍历:    4 -> 2 -> 5 -> 1 -> 3 -> 6    |---------|    |    |----|      左子树    根节点  右子数

The root node and the left and right subtree are found in the sequence traversal and the middle sequence traversal, and the tree can be built next.

Time complexity: O (N)

Space complexity: O (N)

1 class Solution2 {3  Public:4     TreeNode *Buildtree(Vector<int> &Preorder, Vector<int> &inorder)5     {6         return Buildtree(Preorder, 0, Preorder.size() - 1, 7                          inorder, 0, inorder.size() - 1);8     }9 Private:Ten     //Preorder: sequence of pre-sequence traversal, PL and PR: the left and right endpoints of the tree in the sequence of the pre -sequence traversal; One     //inorder: sequence traversal sequences, IL and IR: Middle sequence traversal the left and right end of the tree in the sequence.  A     TreeNode *Buildtree(Vector<int> &Preorder, int PL, int PR,  -                         Vector<int> &inorder, int il, int ir) -     { the         if(PL > PR && il > ir) -             return NULL; -          -         int m; +         //Find the root node location in the middle sequence traversal sequence -          for(m = il; m <= ir && Preorder[PL] != inorder[m]; ++ m); +          A         TreeNode *T = New TreeNode(Preorder[PL]); at         T - Left = Buildtree(Preorder, PL + 1, PL + m - il,  -                             inorder, il, m - 1); -         T - Right = Buildtree(Preorder, PL + m - il + 1, PR,  -                              inorder, m + 1, ir); -         return T; -     } in };

Reprint please indicate source: Leetcode---105. Construct Binary Tree from preorder and inorder traversal

Leetcode---105. 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.