[Leet Code] Binary Tree Inorder Traversal

Input: root = [1,null,2,3]
Output: [1,3,2]
Input: root = []
Output: []
Input: root = [1]
Output: [1]
Input: root = [1,2]
Output: [2,1]
Input: root = [1,null,2]
Output: [1,2]
  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100
# 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 inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
listofelements = []

self.inorder(root, listofelements)

return listofelements
def inorder(self, root, listofelements):

if root:
self.inorder(root.left,listofelements)
listofelements.append(root.val)
self.inorder(root.right, listofelements)

--

--

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!