[Leetcode]Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:linux创建一个文件夹 编辑:程序博客网 时间:2024/06/09 15:52
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: # @param inorder, a list of integers # @param postorder, a list of integers # @return a tree node def buildTree(self, inorder, postorder): if not inorder: return None # inorder is empty root = TreeNode(postorder[-1]) rootPos = inorder.index(postorder[-1]) root.left = self.buildTree(inorder[ : rootPos], postorder[ : rootPos]) root.right = self.buildTree(inorder[rootPos + 1 : ], postorder[rootPos : -1]) return root但是一直Memory Limit Exceeded~后参照别人的代码,改进成如下才AC~在找中序遍历的root index时改用了dictionary来查找(而不是直接用list.index()),这种改进把算法的时间复杂度从O(n ^ 2) 提升到O(n)~还有就是注意一下细节~
class Solution: # @param inorder, a list of integers # @param postorder, a list of integers # @return a tree node def buildTree(self, inorder, postorder): if inorder is None or postorder is None or len(inorder) == 0 or len(postorder) == 0: return None dict = {} for i in xrange(len(inorder)): dict[inorder[i]] = i return self.helper(inorder, postorder, dict, 0, len(inorder) - 1, 0, len(inorder) - 1) def helper(self, inorder, postorder, dict, inL, inR, postL, postR): if inL > inR or postL > postR: return None root = TreeNode(postorder[postR]) index = dict[postorder[postR]] root.left = self.helper(inorder, postorder, dict, inL, index - 1, postL, postL + index - inL - 1) root.right = self.helper(inorder, postorder, dict, index + 1, inR, postR - inR + index ,postR - 1) return root
0 0
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode - Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- IOS多线程
- godot脚本GDScript的使用01
- fedora 20 安装搜狗拼音输入法
- 每个程序员都应该读的书
- 多线程练习--请用多线程显示,每三个数字为一组,直到30.
- [Leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- mybatis快速入门
- 千锋科技为软件工程和软件专业学生开展实训
- 好习惯
- linux设备调试信息输出
- sleep() 和 yield()方法的区别
- nyoj 44 子串和 【DP】
- 如果要使用复杂功能查询,请使用存储过程
- Q3.1 Describe how you could use a single array to implement three stacks