C# Solution


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

Log in to reply
 

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