美文网首页
Pre-order Traversal Of Binary Tr

Pre-order Traversal Of Binary Tr

作者: GakkiLove | 来源:发表于2018-04-24 21:00 被阅读0次

    Implement an iterative, pre-order traversal of a given binary tree, return the list of keys of each node in the tree as it is pre-order traversed.

    Examples

            5
    
          /    \
    
        3        8
    
      /   \        \
    
    1      4        11
    

    Pre-order traversal is [5, 3, 1, 4, 8, 11]

    class Solution(object):
      def preOrder(self, root):
        if not root:
          return []
        res = []
        self.helper(root,res)
        return res
      
      def helper(self,root,res):
        if not root:
          return
        res.append(root.val)
        self.helper(root.left,res)
        self.helper(root.right,res)
    

    相关文章

      网友评论

          本文标题:Pre-order Traversal Of Binary Tr

          本文链接:https://www.haomeiwen.com/subject/jjrslftx.html