My simple C# BFS solution


  • 0
    S
    public class Solution {
        public IList<int> RightSideView(TreeNode root) 
        {
            var result = new List<int>();
            if(root==null)
                return result;
            var queue = new Queue<TreeNode>();
            queue.Enqueue(root);
            while(queue.Count > 0)
            {
                var tq = new Queue<TreeNode>();
                while(queue.Count>0)
                {
                    var node = queue.Dequeue();
                    if(queue.Count() == 0)
                    {
                        result.Add(node.val);
                    }
                    if(node.left != null)
                    {
                        tq.Enqueue(node.left);
                    }
                    if(node.right != null)
                    {
                        tq.Enqueue(node.right);
                    }
                }
                queue = tq;
            }
            return result;
        }
    }
    

Log in to reply
 

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