力扣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
  • 迭代
  1. 用栈进行前序遍历,顺序是根右左
def preorderTraversal(self,root):
	res = []
	if root is None:
		return res
	stack = [root]
	while stack:
		pop_node = stack.pop()
		res.append(pop_node.val)
		if pop_node.right is not None:
			stack.append(pop_node.right)
		if pop_node.left is not None:
			stack.append(pop_node.left)
	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
  • 迭代
class Solution:
    def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        res = []
        stack = [root]
        while stack:
	        pop_node = stack.pop()
			res.append(pop_node.val)
			if pop_node.left is not None:
				stack.append(pop_node.right)
			if pop_node.right is not None:
				stack.append(pop_node.left)
        return res[::-1]
  • 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
  • 迭代方式
  1. 需要借助一个指针,并且一上来不需要把self.root先放到stack里.
  2. 先找到树的左边的节点,依次压入占中,直到找到最左边的节点,cur循环结束
  3. 将最左边的节弹出后,然后加入res
  4. 若当前节点存在右节点,则令cur等于右节点
def inorder_stack(self, root:TreeNode):
	res,stack = [], []
	if root is None:
		return res
	cur = root
	while stack or cur:
		while cur:
			stack.append(cur)
			cur = cur.left
		pop_node = stack.pop()
		res.append(pop_node)
		cur = pop_node.right
	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.将有序数组转换为二叉搜索树
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
力扣是一个在线的编程题库,在其中有各种算法和数据结构的题目,供程序员进行练习。力扣题库支持多种编程语言,包括Python力扣Python格式是指在力扣平台上使用Python语言解答问题时需要注意的一些细节和规范。以下是一些力扣Python格式的要点: 1. 导入模块:根据题目需要,导入相应的Python模块。常见的模块如:math、collections等。 2. 主函数:在解题时,将代码写在一个主函数中。通常命名为def main()。 3. 输入输出:遵循力扣的输入输出格式。使用input函数获取输入数据,使用print函数输出结果。 4. 命名规范:遵循Python的命名规范。变量和函数名采用小写字母与下划线的组合,以便于代码的可读性。 5. 注释:在关键代码处添加注释,描述代码功能和思路。这不仅方便自己理解和维护代码,也方便他人阅读。 6. 缩进:使用统一的缩进风格,通常为4个空格或者1个制表符。 7. 算法实现:根据题目要求,选择合适的算法进行实现。可以使用循环、条件判断、递归等常见的编程结构。 8. 异常处理:对于可能出现异常的地方,使用try-except语句进行异常处理。 9. 提交代码:在完成代码编写后,将代码复制到力扣平台的代码编辑器中,然后点击提交按钮进行代码评测。 总之,力扣Python格式主要是指在力扣平台上使用Python语言解题时需要遵守的编码规范和格式要求。遵循这些规范可以提高代码的可读性和可维护性,从而更好地解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值