力扣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.将有序数组转换为二叉搜索树

你可能感兴趣的:(python学习,leetcode,python,深度优先)