C# Solution


  • 0
    public IList<int> LargestValues(TreeNode root) {
            List<int> result = new List<int>();
            Queue<TreeNode> queue = new Queue<TreeNode>();
            int currentMax = int.MinValue, currentLevel = 0;
            TreeNode currentNode = null;
            
            if (root != null)
            {
                queue.Enqueue(root);
                
                while(queue.Count != 0)
                {
                    currentLevel = queue.Count;
                    
                    while(currentLevel != 0)
                    {
                        currentNode = queue.Dequeue();
                        
                        if (currentNode.val > currentMax)
                            currentMax = currentNode.val;
                            
                        if (currentNode.left != null)
                            queue.Enqueue(currentNode.left);
                        
                        if (currentNode.right != null)
                            queue.Enqueue(currentNode.right);
                            
                        currentLevel--;
                    }
                    
                    result.Add(currentMax);
                    currentMax = int.MinValue;
                }
            }
            
            return result;
        }

Log in to reply
 

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