C# Solution


  • 0
        public IList<IList<int>> LevelOrderBottom(TreeNode root) {
            IList<IList<int>> result = new List<IList<int>>();
            Queue<TreeNode> queue = new Queue<TreeNode>();
            Stack<List<int>> stack = new Stack<List<int>>();
            List<int> tempList = null;
            TreeNode currentNode = null;
            int count = 0;
            
            if (root != null)
            {
                queue.Enqueue(root);
                
                while(queue.Count != 0)
                {
                    count = queue.Count;
                    tempList = new List<int>();
                    
                    while (count-- > 0)
                    {
                        currentNode = queue.Dequeue();
                        tempList.Add(currentNode.val);
                        if (currentNode.left != null)
                            queue.Enqueue(currentNode.left);
                        if (currentNode.right != null)
                            queue.Enqueue(currentNode.right);
                    }
                    
                    stack.Push(tempList);
                }
                
                while (stack.Count != 0)
                    result.Add(stack.Pop());
            }
            
            return result;
        }

Log in to reply
 

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