[Leet Code] Binary Tree Postorder Traversal

Input: root = [1,null,2,3]
Output: [3,2,1]
Input: root = []
Output: []
Input: root = [1]
Output: [1]
Input: root = [1,2]
Output: [2,1]
Input: root = [1,null,2]
Output: [2,1]
# 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]
"""
listOfElements = []
self.postorder(root, listOfElements)
return listOfElements

def postorder(self, root, listOfElements):
if root:
self.postorder(root.left, listOfElements)
self.postorder(root.right, listOfElements)
listOfElements.append(root.val)

--

--

--

Learning, and posting my findings!

Love podcasts or audiobooks? Learn on the go with our new app.

How i cloned an android app using Reality.

STI, Polymorphism and Abstract Classes — Rails

An update on Polymeow

SASS: What’s So Special?

Can I measure that? 4 cases of specialized data you can send to Google Analytics

Creative Construction in the Dwolla Sandbox

Illustration of the Dwolla Sandbox

Configure Tibco EMS JMS Transport Scenario with WSO2 API Manager 3.x

Lootboy Codes November

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matthew Boyd

Matthew Boyd

Learning, and posting my findings!

More from Medium

5/3 binary tree

Programming: Array manipulation using recursion

Level Order Traversal of Binary Tree Using Iteration

Sliding Window #1: Finding the maximum sum of k consecutive elements in an array