Post-order Traversal Of Binary Tree

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

Examples

        5

      /    \

    3        8

  /   \        \

1      4        11

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

class Solution(object):
  def postOrder(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)
    self.helper(root.right,res)
    res.append(root.val)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容