Leetcode, construct binary tree from inorder and post order traversal

Source: Internet
Author: User

Sept. 13, 2015

Spent more than a few hours to work on the leetcode problem, and my favorite blogs about this problems:

1. http://siddontang.gitbooks.io/leetcode-solution/content/tree/construct_binary_tree.html

2. http://blog.csdn.net/linhuanmars/article/details/24390157

After reading the above reference 1, Julia spent first few hours to write the C # implementation:

Https://github.com/jianminchen/Leetcode_C-/blob/master/106ConstructuBTreeFromInorderPostOrderTraversal.cs

In her coding practice of function build (...), she spent over minutes to figure out the coding task:the code to Partit Ion the inorder traversal into and partitions, first is left subtree, and second is right subtree. And then, post order traversal also can being partitioned into the intervals, first one is for left subtree, and then, second One is for right subtree.

She took more than 10-15 minutes to understand the solution. That's too long for real problem solving. Figure out that's only job was to find the root node, and then, it left child and right child are also the root node of the left subtree/or right subtree. The recursive call, actually-them, can help-do the task.

So, she needs-cut down the practice time, and make sure the calculation are correct, easy to tell/maintainable Code/te Stable code. So, she decided to practice it using class Range instead of the arguements start/end integers. Hopefully, this practice'll enhance her memory about partition the array to the parts, one is dividing in mid point, a Nother one is by length of first interval-left subtree.

public class range{
public int start, end;
...

}

Also, she likes to enhance his memory about this solution, so, she writes second implementation using C #, and see if the C Ode can pass online judge. Most important, she tried to the use of different solution, to improve, challenge herself. Write the code without any mistake first time, in less than minutes based on previous one.

Https://github.com/jianminchen/Leetcode_C-/blob/master/106ConstructBTreeFromInOrderPostOrderTraversal_B.cs

Julia likes to train herself using leetcode questions, and biggest problem are to cut down the time to write a solution. She tries to cut the time from hours to 10-30 minutes.

After the code writing, she thought on more about great ideas out there, she should not miss. So, she reads the second reference, and like the most about the analysis:
"This problem and construct Binary tree from preorder and inorder traversal is a difficult problem in the tree, a friend may want to based on the first sequence traversal and post-order traversal can reconstruct the tree, the answer is negative." Only in the order of convenience can be based on the location of the root incision left and right subtree, the other two kinds of traversal can not be done, in fact, the first sequence traversal and post-order traversal is not the only one to determine a tree, there will be ambiguity, that is, two different trees can have the same sequence traversal and post-order traversal, interested friends can

Julia found that in training himself to do the problem is, if you can find a way to improve the speed of writing code, from a few hours, to 10-30 minutes, that is very successful training. One way, is to find her favorite puzzle, to understand the algorithm; Next, see how to improve the speed of writing code, the best way is to write a few more solutions to see which is not prone to error.

Finally, it's a quick look at more than 10 blogs to see if there's a missed, most important, critical analysis.

The next step is to repeat the training; The reason for the analysis timeout, can not reach the goal 10-15 minutes to write the correct code. Just like tennis training, train yourself.

Leetcode, construct binary tree from inorder 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.