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

#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:

Leetcode日练笔记39 [二叉树recursion专题] #104 #101 Maximum Depth of Binary Tree & Symmetric Tree {Python}_第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:

Leetcode日练笔记39 [二叉树recursion专题] #104 #101 Maximum Depth of Binary Tree & Symmetric Tree {Python}_第2张图片 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:

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

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

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:

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

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

Example 2:

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

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:

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

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

你可能感兴趣的:(leetcode日练,leetcode,算法,数据结构,python)