栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

力扣python实现-二叉树合集

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

力扣python实现-二叉树合集

文章目录
  • 二叉树的遍历方式
  • 二叉树的属性
  • 二叉树的修改与构造
  • 求二叉搜索树的属性
  • 二叉树公共祖先问题
  • 二叉搜索树的修改与构造

二叉树的遍历方式
  • 144.二叉树的前序遍历
class Solution:
    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        res = []
        def dfs(root):
            if root is None:
                return
            res.append(root.val)
            dfs(root.left)
            dfs(root.right)
        dfs(root)
        return res
  • 145.二叉树的后序遍历
class Solution:
    def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        res = []
        def dfs(root):
            if root is None:
                return
            dfs(root.left)
            dfs(root.right)
            res.append(root.val)
        dfs(root)
        return res
  • 94.二叉树的中序遍历
class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        res = []
        def dfs(root):
            if root is None:
                return
            dfs(root.left)
            res.append(root.val)
            dfs(root.right)  
        dfs(root)
        return res
  • 102.二叉树的层序遍历
  • 103.二叉树的锯齿形层序遍历
二叉树的属性
  • 104.二叉树的最大深度
class Solution:
    def maxDepth(self, root: TreeNode) -> int:
    	if root is None:
    		return 0
    	return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
  • 111.二叉树的最小深度
class Solution:
    def minDepth(self, root: TreeNode) -> int:
    	if root is None:
    		return 0
    	elif root.left is not None and root.right is not None:
    		return min(self.minDepth(root.left), self.minDepth(root.right)) + 1
    	else:
    		return max(self.minDepth(root.left), self.minDepth(root.right)) + 1
  • 101.对称二叉树
class Solution:
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
    	def isSym(left, right):
    		if left and right:
    			return left.val == right.val and isSym(left.left, right.right) and isSym(left.right, right.left)
    		else:
    			return left == right
    	return isSym(root, root)		
		
  • 110.平衡二叉树
  • 222.完全二叉树的节点个数
  • 257.二叉树的所有路径
  • 404.左叶子之和
  • 513.找树左下角的值
  • 112.路径之和
二叉树的修改与构造
  • 105.从前序与中序遍历序列构造二叉树
class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
    	if not preorder and not inorder:
    		return None
    	root = TreeNode(preorder[0])
    	ind = inorder.index(root)
    	left_node = self.buildTree(preorder[1:ind], inorder[ind+1:])
    	right_node = self.buildTree(preorder[ind+1:], inorder[ind+1:])
    	root.left = left_node
    	root.right = right_node
    	return root   	
  • 106.从中序与后序遍历序列构造二叉树
class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
    	if not inorder and not postorder:
    		return None
    	root = TreeNode(postorder[-1])
    	ind = inorder.index(root)
    	left_node = self.buildTree(inorder[:ind], postorder[:ind])
    	right_node = self.buildTree(inorder[ind+1:], postorder[ind:-1])
    	root.left = left_node
    	root.right = right_node
    	return root
  • 226.翻转二叉树
  • 654.最大二叉树
  • 617.合并二叉树
求二叉搜索树的属性
  • 98.验证二叉搜索树
  • 700.二叉搜索树中的搜索
  • 530.二叉搜索树的最小绝对差
  • 501.二叉搜索树中的众数
  • 538.把二叉搜索树转换为累加树
  • 230.二叉搜索树中第K小的元素
二叉树公共祖先问题
  • 236.二叉树的最近公共祖先
  • 235.二叉搜索树的最近公共祖先
二叉搜索树的修改与构造
  • 701.二叉搜索树中的插入操作
  • 450.删除二叉搜索树中的节点
  • 669.修剪二叉搜索树
  • 108.将有序数组转换为二叉搜索树
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/977627.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号