《从上往下打印二叉树》这道题为什么这样的代码不能通过?

public static ArrayListPrintFromTopToBottom(TreeNode root) 
        ArrayList array = new ArrayList();
         if (root == null) 
             return null; 
        Queue nodes = new LinkedList(); 
        nodes.offer(root);
        while (nodes.size() > 0) { 
                TreeNode pnode = nodes.poll();
                array.add(pnode.val);
                if (pnode.left != null) 
                    nodes.offer(pnode.left); 
                if (pnode.right != null)
                    nodes.offer(pnode.right); 
             }
         if (array.isEmpty()) { 
                 return null;
         } else { 
                 return array;
         }
 }
#Java工程师##算法工程师#
全部评论
已解决代码如下: public class Solution {     public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {          ArrayList array = new ArrayList();          if (root == null)               return array;          Queue<TreeNode> nodes = new LinkedList<TreeNode>();         nodes.offer(root);         while (nodes.size() > 0) {                  TreeNode pnode = nodes.poll();                 array.add(pnode.val);                 if (pnode.left != null)                      nodes.offer(pnode.left);                  if (pnode.right != null)                     nodes.offer(pnode.right);               }          if (array.isEmpty()) {                   return null;          } else {                   return array;          }     } }
点赞
送花
回复 分享
发布于 2015-08-31 23:26
代码怎么是static方法了?
点赞
送花
回复 分享
发布于 2015-08-31 00:22
国泰君安
校招火热招聘中
官网直投
提交之后
点赞
送花
回复 分享
发布于 2015-08-31 19:42

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务