Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3

Output: [1,2,2,3,5,6]

...

Code Block
languagepy
class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
        p = m+n-1
        m -= 1
        n -= 1
        while p>=0:
            if n<0:
                break;
            elif m<0:
                nums1[p] = nums2[n]
                n-=1
            elif nums1[m]<nums2[n]:
                nums1[p] = nums2[n]
                n-=1
            else:
                nums1[p] = nums1[m]
                m-=1
            p-=1


Minimum Depth of Binary Tree

Info

Given binary tree [3,9,20,null,null,15,7],

3
/ \
9 20
/ \
15 7
return its minimum depth = 2.


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

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if root==None:
            return 0
        
        if root.left==None and root.right==None: return 1
        elif root.left!=None and root.right!=None:
            return min( self.minDepth(root.left), self.minDepth(root.right)) + 1
        elif root.left==None:
            return self.minDepth(root.right) + 1
        else:
            return self.minDepth(root.left) + 1