Python iterative easy to read


  • 0
    R
    class Solution(object):
        def rightSideView(self, root):
            if not root: 
                return []
            else:
                res, stk = [], []
                stk.append((root, 0))
                while stk:
                    node, cur = stk.pop()
                    if len(res) == cur:
                        res.append(node.val)
                    if node.left:
                        stk.append((node.left, cur + 1))
                    if node.right:
                        stk.append((node.right, cur + 1))
                return res
    

Log in to reply
 

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