Given the root of 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.
public List<Integer> rightSideView(TreeNode root) {
if (Objects.isNull(root)) {
return new ArrayList<>();
}
List<Integer> rightResult = rightSideView(root.right);
List<Integer> leftResult = rightSideView(root.left);
ArrayList<Integer> result = new ArrayList<>(Math.max(rightResult.size(), leftResult.size()) + 1);
result.add(root.val);
int i = 0;
for (; i < rightResult.size(); i++) {
result.add(rightResult.get(i));
}
for (; i < leftResult.size(); i++) {
result.add(leftResult.get(i));
}
return result;
}