Skip to main content

post-order-traversal

Problem Description​

Given the root of a binary tree, return the postorder traversal of its nodes' values.

Examples​

Example 1: alt text

Input: root = [1,null,2,3]
Output: [1,2,3]

Example 2:

Input: root = []
Output: []

Example 3:

Input: root = [1]
Output: [1]

Constraints​

  • The number of nodes in the tree is in the range [0, 100].
  • -1000 <= Node.val <= 1000

Solution for Binary Tree Post Order Traversal​

Intuition​

  • Recursive is very easy try with iterative way

Code in Different Languages​

Written by @parikhitkurmi
//python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
stack=[]
def dfs(root):
if root:
dfs(root.left)
dfs(root.right)
stack.append(root.val)
dfs(root)
return stack


References​