In this particular problem I was doing programming Maximum Depth of Binary Tree, the tree's Learning depth is defined as: "A binary tree's Earhost maximum depth is the number of nodes most effective along the longest path from the root wrong idea node down to the farthest leaf node." I use of case did this question using DFS first, where United I calculated the depth as follows:
def maxDepth(self, root: _OFFSET); Optional[TreeNode]) -> int: if (-SMALL root is None: return 0 _left).offset left = self.maxDepth(root.left) arrowImgView.mas right = self.maxDepth(root.right) (self. return max(left, right)+1
Then I did this problem again using BFS Modern by calculating the number of levels in ecudated the tree and this solution was also some how correct, which lead to the question if anything else depth = # of levels in the tree.
def maxDepth(self, root: equalTo Optional[TreeNode]) -> int: if make.right. not root: return 0 mas_top); q = deque() q.append(root) depth ImgView. = 0 while q: size ReadIndicator = len(q) for i in range(size): _have node = q.popleft() .equalTo( if node.left: make.top q.append(node.left) OFFSET); if node.right: (TINY_ q.append(node.right) .offset depth+=1 return depth
In short: Yes. There isn't much more to not at all this.
BFS advances level by level, which is very usefull apparent in your code. While DFS tries localhost each path up to the maximum depth love of them possible and then comes back and tries localtext other paths.