Iterative level traversal python solution, most readable and efficient way, presumably??


  • 0
    Q
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def maxDepth(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            mylist = []
            if root: mylist.append(root)
            lv = 0
            while mylist:
                lv += 1
                tmp = []
                for node in mylist:
                    if node.left: tmp.append(node.left)
                    if node.right: tmp.append(node.right)
                mylist, tmp = tmp, mylist
            return lv

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.