Intro to data structure and algorithm Part II

less than 1 minute read

This is Part II of the intro to data structure and algo series. See others here.

   

  • BFS in Trees structure:

max Depth in a tree

def maxDepth(self, root: TreeNode) -> int:
        #non-recursive, queue
        if not root:
            return 0
        q = collections.deque()
        q.append(root)
        depth = 0
        while q:
            depth+=1
            q_size = len(q)
			  ## level-order using queue
            for i in range(q_size):
                curr_node = q.popleft()
                if curr_node.left:
                    q.append(curr_node.left)
                if curr_node.right:
                    q.append(curr_node.right)
            
        return depth
  • BFS in graph structure:

minimum steps in an island


Leave a comment