Leetcode: Binary tree inorder traversal
The task is to traverse a binary tree inorder. This means, that starting from the root node, nodes should be visited in the following order: left, self, right. With this knowledge it is easy to come up with a recursive algorithm.
The only special case that needs to be handled are the leaf nodes that don’t have any children. By still visiting the non-existing children and then returning an empty list (
) when a
None is encountered, the whole algorithm fits into two lines.
Runtime: 41 ms, faster than 70.32% of Python3 online submissions for Binary Tree Inorder Traversal.
Memory Usage: 13.8 MB, less than 60.08% of Python3 online submissions for Binary Tree Inorder Traversal.