By Long Luo

Here shows 2 Approaches to slove this problem: DFS and BFS.

# DFS

We traverse the tree in the order of $\textit{root node} \to \textit{right subtree} \to \textit{left subtree}$ to ensure that each level traverse the rightmost node first.

## Analysis

• Time Complexity: $O(n)$
• Space Complexity: $O(n)$

# BFS

We can use BFS to traverse the levels of the tree and record the last element of each level.

## Analysis

• Time Complexity: $O(n)$.
• Space Complexity: $O(n)$.

All suggestions are welcome.
If you have any query or suggestion please comment below.
Please upvote👍 if you like💗 it. Thank you:-)

Explore More Leetcode Solutions. 😉😃💗