Skip to content

Instantly share code, notes, and snippets.

@atiq-cs
Last active January 30, 2019 01:43
Show Gist options
  • Save atiq-cs/d20172ae600be4b345a75c45f71a59e7 to your computer and use it in GitHub Desktop.
Save atiq-cs/d20172ae600be4b345a75c45f71a59e7 to your computer and use it in GitHub Desktop.
Get right side view of a binary tree
// O(N) Time , O(lg N) Space
public class Solution {
IList<int> nodeList = null;
public IList<int> RightSideView(TreeNode root) {
nodeList = new List<int>();
FindRight(root);
return nodeList;
}
private void FindRight(TreeNode root, int depth = 0) {
if (root == null)
return ;
if (nodeList.Count == depth)
nodeList.Add(root.val);
FindRight(root.right, depth + 1);
FindRight(root.left, depth + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment