Leetcode日练笔记39 [二叉树recursion专题] #104 #101 Maximum Depth of Binary Tree & Symmetric Tree {Python}

这篇博客主要介绍了LeetCode上的两道二叉树题目:104. 最大深度的二叉树和101. 对称树。通过递归方法解决,详细阐述了解题思路和运行时间。最大深度问题采用自顶向下的方法,而对称树问题利用递归从根节点开始进行比较。
摘要由CSDN通过智能技术生成

#104 Maximum Depth of Binary Tree

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: 3

Example 2:

Input: root = [1,null,2]
Output: 2

解题思路:

1.top_down(root, level)

先得到root节点,这题里level算成1(从例题得知)。那么其子叶的level默认加一。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root: return 0
        global ans
        ans = -1
        
        def helper(root, level):
            
            if not root: return
            global ans
            if not root.left and not root.right: ans = max(ans, level)
                
            helper(root.left, level+1)
            helper(root.right, level+1)
            
           
        helper(root,1)
        return ans

runtime:

 2.bottom_up(root)

对于每个节点,其所在的level等于左叶的level最大值与右叶level最大值,两者取的最大值再加一。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root: return 0
        l = self.maxDepth(root.left)
        r = self.maxDepth(root.right)
        
        return max(l,r) + 1

runtime:

 第二种写起来简单,但是跑起来慢好多。

iteration的思路留给第二次系统刷题的时候做。现在主攻recursion的练习。

#101 Symmetric Tree

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Example 1:

Input: root = [1,2,2,3,4,4,3]
Output: true

Example 2:

Input: root = [1,2,2,null,3,null,3]
Output: false

解题思路:

因为要从母节点开始比较,所以适合用top down的办法。

params选择root的左叶, l, 和右叶, r, 传给下一次循环判断。

最简单的base case,分别有三种情况:

1. 只有一个母节点,无左叶和右叶。直接返回True

2.有左叶和右叶,需要做三次bool值的判断:

        1)两个值是否相等;

        2)再call一次helper function,params为左叶的左叶和右叶的右叶

        3)第二次call helper function,params为左叶的右叶和右叶的左叶

当三个bool值都为True的时候,才可以返回True。反之,则False

3. 只有左叶或右叶,直接返回False

最后返回 helper(root.left,root.right)的bool值。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        # check if the values of left pointer and right pointer are the same
        def helper(l, r):
            
            # if both nodes are None, return True
            if not l and not r:
                return True
            
            # if both nodes exist, compare their values
            elif l and r:
                # True if they are the same
                return ((l.val == r.val) and helper(l.left, r.right) and helper(l.right, r.left))
            
            # one exists, the other does not
            else:
                return False
            
    
        return helper(root.left, root.right)

runtime:

iterative的思路还是留给第二次系统刷题,当复习。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值