An algorithm for binary tree structure with known binary tree's pre-sequence traversal, middle sequence traversal or middle sequence traversal and post-order traversal

Source: Internet
Author: User

The pre-order traversal in the binary tree is the first access to the root node, then the left subtree and the right subtree.

The middle sequence traversal is the first access to the left subtree, then the root node, and finally the right sub-tree.

The post-order traversal is the first to access the left subtree, then the right subtree, and finally the root node.

The algorithm is based on the first node of the pre-sequence traversal or the last node of the post-order traversal, find the corresponding position in the middle sequence traversal, you can determine the left subtree contains the elements and the right subtree contains elements, and finally through the implementation of the recursive return can be.

The binary tree is represented in the form

The structure of the binary tree is expressed as class treenode{    int val;    TreeNode left;    TreeNode right;    TreeNode (int x) {val = x;}}

Known pre-sequence traversal, the middle order traversal to find the binary tree structure, the code is as follows:
Class solution{    private TreeNode __buildtree (int[] preorder, int start1, int[] inorder, int start2, int len)    {
   
    if (0 = = len) return null;        TreeNode root = new TreeNode (Preorder[start1]);        Find the root node in the middle sequence traversal, determine the Saozi right subtree        int index = START2;        for (int i = Start2; I < Start2 + len; i++)        {            if (inorder[i] = = Preorder[start1])            {                index = i;                break;            }        }        int len1 = Index-start2;        Root.left = __buildtree (preorder, Start1 + 1, inorder, Start2, len1);        Root.right = __buildtree (preorder, Start1 + 1 + len1, inorder, index + 1, len-1-len1);        return root;    }    Public TreeNode Buildtree (int[] preorder, int[] inorder)    {        return __buildtree (preorder, 0, inorder, 0, preorder.length);}    }
   

Known in sequence traversal, post-order traversal, the binary tree structure code is as follows:

Class solution{    private TreeNode __buildtree (int[] inorder, int start1, int[] postorder, int start2, int len)    {
   if (0 = = len) return null;        int val = postorder[start2 + len-1];        TreeNode root = new TreeNode (val);        Find the root node in the middle sequence traversal, determine the Saozi right subtree        int index = START1;        for (int i = Start1; I < Start1 + len; i++)        {            if (inorder[i] = = val)            {                index = i;                break;            }        }        int len1 = Index-start1;        Root.left = __buildtree (inorder, Start1, Postorder, Start2, len1);        Root.right = __buildtree (inorder, Start1 + 1 + len1, postorder, Start2 + len1, len-1-len1);        return root;    }    Public TreeNode Buildtree (int[] inorder, int[] postorder)    {        return __buildtree (inorder, 0, postorder, 0, postorder.length);}    }



Algorithm for binary tree structure with known binary tree's pre-sequence traversal, middle sequence traversal or middle sequence traversal and post-order 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.