[LeetCode]#94. Binary Tree Inorder Traversal

Fatboy Slim
Jul 23, 2021

--

Environment: Python 3.8

Key technique: binarytree, Node

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

Example 1:

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

Analysis:

  1. Please see leetcode solution, it is very clear.

Solution:

from binarytree import Node
root = Node(1)
root.right = Node(2)
root.right.left=Node(3)
# Getting binary tree
print('Binary tree :', root)
#root = [1,null,2,3]
class Solution:
def inorderTraversal(self, root):
if root:
return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)
else:
return []

Submissions:

Reference:

https://leetcode.com/problems/binary-tree-inorder-traversal/discuss/1348586/Python-One-Liner

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet