Intuition
We need to the structure of the given binary tree such that all of it’s nodes have no left property, and the tree is built only on right nodes.
Here’s how I thought about it: a tree with only right nodes is a linked list. Think of node.right
of a tree as the same as node.next
of a linked list.
So we extract the nodes from the tree using in-order traversal
and store them in a list.
Then we build a linked list recursively treating the node.right
pointer as a node.next
.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since extracting the tree is a linear process and so is building the linked list.
-
Memory
The memory usage is since we storing all nodes in a list.
— A