ecs


  • 0
      var array = [] ; 
    var averageOfLevels = function(root) {
      
          if (root!=null) {
              array.push(root.val) 
              
              
                cal   (root)  ; 
       
        return array ; 
          } 
         
        return null ; 
      
    };
    
    
    var cal = function (root){
            if (root.left == null && root.right !=null )   
                
                array.push(root.right.val);  
            else if (root.right == null && root.left !=null )   
                   
                 array.push(root.left.val);  
            
        
            else if (root.right != null && root.left !=null )    {
                 var average = (root.left.val + root.right.val)/2 ;  
                   array.push(average);  
    
                  var left =  cal(root.left);
              var right =  cal(root.right);
    
    
    
            }else 
              //root.right == null && root.left ==null
                  return ; 
        
       
        
    }
    
    

Log in to reply
 

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