Last active
January 30, 2019 01:43
-
-
Save atiq-cs/d20172ae600be4b345a75c45f71a59e7 to your computer and use it in GitHub Desktop.
Get right side view of a binary tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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