C# Solution


  • 0
    public IList<int> RightSideView(TreeNode root) {
            List<int> result = new List<int>();
            Queue<TreeNode> level = new Queue<TreeNode>();
            int countInLevel = 0;
            TreeNode currentNode = null;
            
            if (root != null)
            {
                level.Enqueue(root);
                
                while (level.Count != 0)
                {
                    countInLevel = level.Count;
                    
                    while (countInLevel > 0)
                    {
                        currentNode = level.Dequeue();
                        if (currentNode.left != null)
                            level.Enqueue(currentNode.left);
                        if (currentNode.right != null)
                            level.Enqueue(currentNode.right);
                            
                        if (countInLevel == 1)
                            result.Add(currentNode.val);
                        
                        countInLevel--;
                    }
                }
            }
            
            return result;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.