2
// Utility Function of RightSideView3
public void rightView(TreeNode curr, List<Integer> list, int level) {4
// if, current is null, return9
// if, level = list size10
// add current val to list11
if (level == list.size()) {15
// recursive call for right side view16
rightView(curr.right, list, level + 1);17
// recursive call for left side view18
rightView(curr.left, list, level + 1);21
// Binary Tree Right Side View Function22
public List<Integer> rightSideView(TreeNode root) {24
List<Integer> result = new ArrayList<>();25
// call right view function26
rightView(root, result, 0);33
Input: root = [1,2,3,null,5,null,4]37
// Time & Space Complexity -40
Space - O(h) h = height of binary tree