【Leetcode】【python】Construct Binary Tree from Preorder and Inorder Traversal (Inorder and Postorder)
来源:互联网 发布:mac第三方应用商店 编辑:程序博客网 时间:2024/05/22 03:40
题目大意
根据二叉树的前序遍历和中序遍历( 中序和后序)结果生成二叉树
假设没有重复数字
解题思路
参考给中序和后序遍历
看到树首先想到要用递归来解题。以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去。由于后序遍历的最后一个节点就是树的根。也就是root=1,然后我们在中序遍历中搜索1,可以看到中序遍历的第四个数是1,也就是root。根据中序遍历的定义,1左边的数{4,2,5}就是左子树的中序遍历,1右边的数{6,3,7}就是右子树的中序遍历。而对于后序遍历来讲,一定是先后序遍历完左子树,再后序遍历完右子树,最后遍历根。于是可以推出:{4,5,2}就是左子树的后序遍历,{6,3,7}就是右子树的后序遍历。而我们已经知道{4,2,5}就是左子树的中序遍历,{6,3,7}就是右子树的中序遍历。再进行递归就可以解决问题了。
代码
前序和中序
class Solution(object): def buildTree(self, preorder, inorder): if len(preorder) == 0: return None if len(preorder) == 1: return TreeNode(preorder[0]) root = TreeNode(preorder[0]) index = inorder.index(root.val) # 中序中根节点的位置,左边即为左子树,右边由子树 root.left = self.buildTree(preorder[1 : index + 1], inorder[0 : index]) root.right = self.buildTree(preorder[index + 1 : len(preorder)], inorder[index + 1 : len(inorder)]) return root
中序和后序
class Solution(object): def buildTree(self, inorder, postorder): if len(inorder) == 0: return None if len(inorder) == 1: return TreeNode(inorder[0]) root = TreeNode(postorder[len(postorder) - 1]) index = inorder.index(postorder[len(postorder) - 1]) root.left = self.buildTree(inorder[ 0 : index ], postorder[ 0 : index ]) root.right = self.buildTree(inorder[ index + 1 : len(inorder) ], postorder[ index : len(postorder) - 1 ]) return root
总结
二叉树遍历
二叉树的前序、中序、后序遍历(深度优先遍历)
遍历即将树的所有结点都访问且仅访问一次。按照根结点访问次序的不同,可以分为前序遍历,中序遍历,后序遍历。
前序遍历:根结点 -> 左子树 -> 右子树
中序遍历:左子树 -> 根结点 -> 右子树
后序遍历:左子树 -> 右子树 -> 根结点
前序遍历:abdefgc
中序遍历:debgfac
后序遍历:edgfbca层次遍历(广度优先遍历)
层次遍历:abcdfeg
阅读全文
0 0
- 【Leetcode】【python】Construct Binary Tree from Preorder and Inorder Traversal (Inorder and Postorder)
- 【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder/Preorder and Inorder Traversal
- leetCode(18):Construct Binary Tree from Preorder and Inorder (Inorder and Postorder) Traversal
- LeetCode 105, 106. Construct Binary Tree from Preorder and Inorder/Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Preorder(Postorder) Traversal
- Construct Binary Tree from Preorder (Postorder) and Inorder Traversal
- *(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 Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder 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
- VTK之自定义Filter类
- JavaScript函数式编程(一)
- numpy函数(1)——linspace创建等差数列
- 《数据结构学习与实验指导》5-5:QQ账号的申请与登录
- 动态库与静态库
- 【Leetcode】【python】Construct Binary Tree from Preorder and Inorder Traversal (Inorder and Postorder)
- QQ怎么找回/恢复被删掉的好友
- 2122数据结构实验之链表七:单链表中重复元素的删除
- 二叉树的基本操作
- 35.查找插入位置
- 第二次作业
- JavaScript函数式编程(二)
- leetcode 241. Different Ways to Add Parentheses
- tensorflow安装卸载