Leetcode_105_construct Binary Tree from preorder and inorder traversal

Source: Internet
Author: User

Describe:

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

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

Idea: 1. Store the middle sequence traversal sequence and its corresponding subscript in a map to facilitate the following lookup 2. Recursively selects the first element of the pre-order sequence as the root node of the tree, and then finds the root node in the ordinal sequence of the position inorderindex,inorderindex-startinorder can be Zuozi Length 3. Based on the length and startpreorder of the Zuozi, the starting position of the left subtree in the pre-order sequence can be obtained 4. From above, we can find the first sequence of the left and right sub-tree and the starting position of the middle sequence sequence, and call the achievement process recursively. PS: In fact, for this problem, there is a more simple method, according to the sequence of elements appearing sequentially as the root node of the tree in order to build, so much simpler, do not have to calculate the pre-sequence and the sequence of the right and left sub-tree starting position, which day the code to fill the method. My method is very complex, but, I feel more direct, but also, pre-order in order to build the sequence can be. Code:
/** * Definition for Binary tree * public class TreeNode {* int val, * TreeNode left, * TreeNode right;    TreeNode (int x) {val = x;}} */public class solution {int []arrpreorder;int []arrinorder; Map<integer, Integer>mapinorder=new Hashmap<integer, integer> ();p ublic TreeNode buildtree (int[] Preorder , int[] inorder) {if (preorder.length==0| | Preorder==null) return null;    Arrpreorder=preorder;    Arrinorder=inorder;        for (int i=0;i<inorder.length;i++) mapinorder.put (Inorder[i], i);        int start=0, end=preorder.length-1;        TreeNode Root =new TreeNode (0);        Createtree (Root,start,end,start, end);    return root; }public void Createtree (TreeNode root, int start1,int end1,int start2,int end2) {int Substart1=start1,substart2=start2, Subend1=end1,subend2=end2;int target=arrpreorder[start1];int indexinorder=mapinorder.get (target); int len= Indexinorder-start2;int indexpreorder=start1+len;if (start2<=indexinorder-1) {SubStart1=start1+1;sUbend1=indexpreorder;subend2=indexinorder-1;root.left=new TreeNode (0); Createtree (Root.left,substart1,start2, SubStart2, SubEnd2);} Root.val=target;if (indexinorder+1<=end2) {substart1=indexpreorder+1;substart2=indexinorder+1;root.right=new TreeNode (0); Createtree (Root.right,substart1,end1, SubStart2, End2);}}
Results:

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.