Pre-order Traversal Of Binary Tr

2018-04-24  本文已影响0人  GakkiLove

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)
上一篇 下一篇

猜你喜欢

热点阅读