Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
Input: [1,2,3,null,5,null,4] Output: [1, 3, 4] Explanation: 1 <--- / \ 2 3 <--- \ \ 5 4 <---
We use postorder tree traversal.
- We maintain the current depth in the recurrent calls;
- Use vector
valsto store rightmost value at depth
- The nodes at the same depth will be visited from left to right. The key to ensure this is to traverse left subtree before the right one.
This will allow to overwrite the value at the same depth in
- Is is easy to see that preoreder or inorder traversal would work as well.