为什么在线Java IDE解决方案抛出“错误:<identifier>expected”?
我在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;
}
}
# 1 楼答案
long
是java中的一个关键字,不应将其用作参数名