Java for Leetcode 106 Construct Binary Tree from inorder and Postorder traversal

Source: Internet
Author: User

Construct Binary Tree from inorder and Postorder traversal

Total Accepted: 31041 submissions: 115870

Given Inorder and Postorder traversal of a tree, construct the binary tree.

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

Problem Solving Ideas:

Modify the following code to solve the problem, the Java implementation is as follows:

static public TreeNode Buildtree (int[] inorder, int[] postorder) {return Buildtree (postorder, inorder, 0, Postorder.lengt H-1, 0, inorder.length-1);      }      static public TreeNode Buildtree (int[] postorder, int[] inorder, int pbegin, int pEnd, int ibegin, int iend) {          if (pbegi n>pend)              return null;          TreeNode root = new TreeNode (Postorder[pend]);          int i = Ibegin;          for (; i<iend;i++)            if (inorder[i]==root.val) break                  ;          int lenleft = I-ibegin;          Root.left = Buildtree (Postorder, Inorder, Pbegin, Pbegin+lenleft-1, Ibegin, i-1);          Root.right = Buildtree (Postorder, Inorder, Pbegin+lenleft, PEnd-1, i+1, iend);          return root;    }

Java for Leetcode 106 Construct Binary Tree from inorder and Postorder 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.