美文网首页
In-order Traversal Of Binary Tre

In-order Traversal Of Binary Tre

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

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

            5
    
          /    \
    
        3        8
    
      /   \        \
    
    1      4        11
    

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

    Corner Cases

    What if the given binary tree is null? Return an empty list in this case.

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

    相关文章

      网友评论

          本文标题:In-order Traversal Of Binary Tre

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