[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.

Double, Double Toil and Trouble

Juggling tech & product: Meet monday.com Mobile Team

Arithmetic & Logical Operators: R Data Types with Example

PostgreSQL Data Manipulation in C

S08E04: finally, someone wants to match things

Service Mesh Journey At Trendyol — Part 1

Information extraction in Scala with Odin (NLP/NLU)

Java is a Great Career to Grow in IT Field

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

WHAT IS JAVA?

Min-Max Sum | HackerRank Problem | Java Solution

Java I/O Byte Stream Implementation

Head First Java Summary (Chapter 2)