leetcode_11: 二叉树的最小深度(111)、路径综合(112)

1.二叉树的最小深度(111)

给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

思路1:递归法
1.此时参数为None,返回0即可,表示并不增加深度
2.左子树节点为空,返回右子节点的最小深度+1。为什么不返回0,因为此时这个节点可能不是叶子节点,还要考察右子树节点
3.右子树节点为空,返回左子节点的最小深度+1。
4.左右子树都有节点,那么返回左右子树中的最小深度,并且+1

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if not root:
            return 0
        if not root.left:
            return self.minDepth(root.right) + 1
        if not root.right:
            return self.minDepth(root.left) + 1
        return min(self.minDepth(root.left), self.minDepth(root.right)) + 1

思路2:深度优先搜索

从一个包含根节点的栈开始,当前深度为 1 。然后开始迭代:弹出当前栈顶元素,将它的孩子节点压入栈中。当遇到叶子节点时更新最小深度。遍历所有的节点,找到最小值。

class Solution:
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        else:
            stack, min_depth = [(1, root),], float('inf')
        
        while stack:
            depth, root = stack.pop()
            children = [root.left, root.right]
            if not any(children):
                min_depth = min(depth, min_depth)
            for c in children:
                if c:
                    stack.append((depth + 1, c))
        
        return min_depth

思路3:广度优先搜索

按照树的层次去迭代(利用队列先进先出的方式),第一个访问到的叶子就是最小深度的节点,这样就不要遍历所有的节点了。

from collections import deque
class Solution:
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        else:
            node_deque = deque([(1, root),])
        
        while node_deque:
            depth, root = node_deque.popleft()
            children = [root.left, root.right]
            if not any(children):
                return depth
            for c in children:
                if c:
                    node_deque.append((depth + 1, c))

知识点:二叉树的搜索:

             第一种方法(深度优先搜索)利用栈的先进后出(while 栈,pop父节点,append子节点)。

             第二种方法(广度优先搜索)利用队列的先进先出(while 队列,popleft, append子节点),这种是按层次进行迭代搜索。

             第三种方法递归将每个节点都作为根节点的方式进行搜索。

参考链接:

https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/solution/er-cha-shu-de-zui-xiao-shen-du-by-leetcode/

https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/solution/di-gui-fa-yan-du-you-xian-sou-suo-python-by-juncao/

2.路径综合(112)

给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。

说明: 叶子节点是指没有子节点的节点。

思路:深度优先搜索

定义栈=(根节点,剩余目标和)。

开始迭代,弹出栈顶元素,判定如果当前元素的剩余目标和为0且没有子节点,则返回true. 否则如果存在子节点,则将子节点和对应剩余目标和压入栈中。

如果遍历完所有根节点到叶子节点的情况均未找到符合的路径,那么返回false.

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def hasPathSum(self, root: TreeNode, sum: int) -> bool:
        if not root:
            return False
       
        stack = [(root, sum-root.val),]
        while stack:
            node, new_sum = stack.pop()
            if not node.left and not node.right and new_sum == 0:
                return True
            if node.right:
                stack.append((node.right,new_sum-node.right.val))
            if node.left:
                stack.append((node.left,new_sum-node.left.val))
        return False

参考链接:https://leetcode-cn.com/problems/path-sum/solution/lu-jing-zong-he-by-leetcode/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值