C# accepted solution


  • 0
    R
    public IList<IList<int>> LevelOrder(TreeNode root)
    {
    	IList<IList<int>> levelOrder = new List<IList<int>>();
    	Queue<TreeNode> queue = new Queue<TreeNode>();
    	if(root!=null) queue.Enqueue(root);
    	while(queue.Count != 0)
    	{
    		var levelSize = queue.Count;
    		List<int> levelNums = new List<int>();
    		for (int i = 0; i < levelSize; i++)
    		{
    			var dequeued = queue.Dequeue();
    			levelNums.Add(dequeued.val);
    			if (dequeued.left != null) queue.Enqueue(dequeued.left);
    			if (dequeued.right != null) queue.Enqueue(dequeued.right);
    		}
    		levelOrder.Add(levelNums);
    	}
    	return levelOrder;
    }

Log in to reply
 

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