Leetcode, construct binary tree from inorder and post order traversal

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 20 minutes to figure out the coding task: the code to partition the inorder traversal into two partitions, first is left subtree, second is right subtree. And then, post order traversal also can be partitioned into two 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 is too long for real problem solving. Figure out that only job is to find the root node, and then, its left child and right child is also the root node of left subtree/ or right subtree. The recursive call, actually two of them, can help to do the task.

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

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

}

Also, she likes to enhance her memory about this solution, so, she writes second implementation using C#, and see if the code can pass online judge. Most important, she tried to use different solution, to improve, challenge herself. Write the code without any mistake first time, in less than 10 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 is to cut down the time to write a solution. She tries to cut down time from hours to 10-30 minutes.

After the code writing, she thought about more about great ideas out there, she should not miss. So, she reads the second reference, and like the most about the analysis:
"这道题和Construct Binary Tree from Preorder and Inorder Traversal是树中难度比较大的题目了,有朋友可能会想根据先序遍历和后序遍历能不能重新构造出树来,答案是否定的。只有中序便利可以根据根的位置切开左右子树,其他两种遍历都不能做到,其实先序遍历和后序遍历是不能唯一确定一棵树的,会有歧义发生,也就是两棵不同的树可以有相同的先序遍历和后序遍历,有兴趣的朋友可以试试举出这种例子."

Julia 发现在训练自己做题是, 如果能摸索出方法, 提高写代码的速度, 从几个小时, 到10-30 分钟, 那就是很成功的训练. 一种方式, 就是, 找到她喜欢的题解, 能够理解算法; 接下来, 看如何提高写代码的速度, 最好的方式, 就是多写几个解法, 看哪个不容易出错.

最后, 就是, 快速看十几个博客, 看有没有错过最重要, 最关键的分析.

接下来, 就是重复训练; 分析超时的原因, 能不能达到目的10-15分钟写出正确的代码. 就像网球训练, 训练自己.

时间: 2024-12-25 08:37:26

Leetcode, construct binary tree from inorder and post order traversal的相关文章

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!! 下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || po

[leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去.由于后序遍历的最后一个节点就是树的根.也就是roo

Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal

Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 递归构建. 思路就是: preorder可以定位到根结点,inorder可以定位左右子树的取值范围. 1. 由

LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. Hide Tags Tree Array Depth-first Search SOLUTION 1: 使

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 这道题要求从中序和后序遍历的结果来重建原二叉树,我们知道中序的遍历顺序是左-根-右,后序的顺序是左-右-根,对于这种树的重建一般都是采用递归来做,可参见我之前的一篇博客Convert Sorted Array to Bin

[Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树

Given inorder and postorder traversal of a tree, construct the binary tree. Note:  You may assume that duplicates do not exist in the tree. 利用中序和后序遍历构造二叉树,要注意到后序遍历的最后一个元素是二叉树的根节点,而中序遍历中,根节点前面为左子树节点后面为右子树的节点.例如二叉树:{1,2,3,4,5,6,#}的后序遍历为4->5->2->6-&

[Leetcode] Construct Binary Tree from Inorder and Postorder Traversal I,II

这两个问题实际上是同一个问题,需要对三种遍历方式的规律非常清楚. 对于前序遍历,第一个元素实际上就是root,然后后面的元素前半部分是左树的node,后半部分是右树的node 对于中序遍历,一旦我们知道了root节点,那么就可以将其分为两半部分,也就是左树和右树 对于后序遍历,我们可以缺点最后一个节点是root节点,剩下的部分:前半部分是左树节点,后半部分是右树节点. 这样使用递归就可以解决,需要使用两组index划定两种遍历的range,当left>right的时候,就需要返回null 有点类

Construct Binary Tree From Inorder and Preorder/Postorder Traversal

map<int, int> mapIndex; void mapToIndex(int inorder[], int n) { for (int i = 0; i < n; i++) { mapIndex.insert(map<int, int>::value_type(inorder[i], i); } } Node* buildInorderPreorder(int in[], int pre[], int n, int offset) { assert(n >=