LeetCode: 106. 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.
For example, given
inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]
Return the following binary tree:
3/ \9 20/ \15 7
解题思路
跟LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal一模一样的解法,只是把前序数组的每次输出第一个换成后序数组输出最后一个。此外,105是先左节点再右节点,106反过来。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution:def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
# while postorder:
# print(postorder.pop())if inorder:root_val = postorder.pop()# print(inorder.index(root_val))ind = inorder.index(root_val)root = TreeNode(root_val)root.right = self.buildTree(inorder[ind+1:], postorder)root.left = self.buildTree(inorder[0:ind], postorder)return root
LeetCode: 106. Construct Binary Tree from Inorder and Postorder Traversal相关推荐
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...
- LeetCode - 106 - Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal | 106. 从中序后序遍历序列构造二叉树(Java)
题目 https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题解 待优化: ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- 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 tha ...
- Construct Binary Tree from Inorder and Postorder Traversal
题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
最新文章
- MindSpore基本原理
- Xamarin Essentials教程获取路径文件系统FileSystem
- IntelliJ IDEA 2020.2.4款 神级超级牛逼插件推荐(自用,真的超级牛逼)
- 13.SpringMVC和Spring集成(一) 14.SpringMVC和Spring集成(二)
- BOOST_VMD_ASSERT宏相关的测试程序
- boost::shared_mutex相关的测试程序
- oracle客户端没有deinstall.bat,Oracle数据库11gR2的卸载 - deinstall
- [转]C++的坑真的多吗?
- 获取数据 - 下载附件解压附件 - Python代码
- python地图标注_Python 给定的经纬度标注在地图上的实现方法
- IP路由故障关于BGP的疑问解答
- mysql5.7存储过程实例_MySQL 存储过程简单实例
- 将一个项目中的图片存到另一个项目中_复盘一个Smart200小项目(2)
- 获取本地ip(一个或多个都能取)
- ArcGIS_重分类
- windows设置开机自动启动程序
- 新手选择蓝牙耳机要注意什么?双11发烧友疯狂推荐五款蓝牙耳机
- 【转】为什么要使用ModelDriven
- 为什么面试要问hashmap 的原理
- 微信开发者工具:代码更新后页面未刷新