[LeetCode]#94. Binary Tree Inorder Traversal

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