代码之家  ›  专栏  ›  技术社区  ›  sofs1 Romain Manni-Bucau

为什么在线java ide解决方案抛出“error:<identifier>expected”?

  •  -1
  • sofs1 Romain Manni-Bucau  · 技术社区  · 6 年前

    我在leetcode.com解决这个问题

    我一点也不想找到解决办法。我不明白,为什么我在这里检查其他答案却得到这个错误。

    问题: 给定一棵二叉树,找出路径中每个节点具有相同值的最长路径的长度。此路径可能通过也可能不通过根。

    注意:两个节点之间的路径长度由它们之间的边数表示。

    例1:

    输入:

              5
             / \
            4   5
           / \   \
          1   1   5
    

    输出:

    2
    

    下面是我的代码

    Line 25: error: <identifier> expected
    

    public int longestUnivaluePathHelper(TreeNode root, int long, int prevLongest, int totalHeight)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    // rule 1: the univalue can be either left side or right side
    // rule 2: it need not go thru root
    // rule 3: height < 1000
    /*
    
            1
         2     3
    
    */
    
    class Solution {
        public int longestUnivaluePath(TreeNode root) {
            return longestUnivaluePathHelper(root,1,1,1);
        }
    
        public int longestUnivaluePathHelper(TreeNode root, int long, int prevLongest, int totalHeight){
            if(totalHeight >= 1000){
                return prevLongest;
            }
            if(root.left == null || root.right == null){
                return prevLongest;
            }else{
                if(root.val == root.left ){
                    long++;                
                }else if(root.val == root.right){
                    long++;
                }else{
                    prevLongest = long;
                    long = 1;
                    totalHeight++;
                }
                longestUnivaluePathHelper(root.left, long, prevLongest, totalHeight);
                longestUnivaluePathHelper(root.right, long, prevLongest, totalHeight);
            }
            //return prevLongest;
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   flyingfox    6 年前

    long 是Java中的关键字,不应将其用作参数名

        2
  •  2
  •   Karol Dowbecki    6 年前

    你不能用 long 对于任何标识符名称,因为它是保留关键字。重命名方法参数,例如

    public int longestUnivaluePathHelper(TreeNode root, int value, int prevLongest, int totalHeight)
    

    看一看 Java Language Keywords 查看保留了哪些关键字。