C# Solution


  • 0
    M
    public IList<IList<int>> LevelOrderBottom(TreeNode root) {
        IList<IList<int>> list = new List<IList<int>>(); 
        PostOrder(root, 0, list);         
        
        IList<IList<int>> reverseList = new List<IList<int>>(); 
        
        for (int i = list.Count - 1; i >= 0; i--)
        {
            reverseList.Add(list[i]); 
        }
        
        return reverseList; 
    }
    
    public void PostOrder(TreeNode root, int height, IList<IList<int>> treeList)
    {
        if (root !=null)
        {
            if (treeList.Count - 1 < height)
            {                
                treeList.Add(new List<int>(){ root.val});                
            }
            else 
            {
                treeList[height].Add(root.val); 
            }   
            
            PostOrder(root.left, height + 1, treeList); 
            PostOrder(root.right, height + 1, treeList);                                              
        }            
    }

Log in to reply
 

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