原价买的衣服但发现网上打折可以price match嘛?# Fashion - 美丽时尚
s*o
1 楼
看似很简单的一个函数,查看一个BST有没有loop,我的代码是,没有那一行“remove
”的代码就会错,我现在还没想明白。。。。
public static boolean cycle(TreeNode root, HashSet visited) {
if (root == null) {
return true;
}
if (visited.contains(root.c)) {
return false;
}
visited.add(root.c);
boolean res = cycle(root.left, visited)&& cycle(root.right, visited);
//没有这一行就会错,为啥??????????
visited.remove(root.c);
return res;
}
”的代码就会错,我现在还没想明白。。。。
public static boolean cycle(TreeNode root, HashSet
if (root == null) {
return true;
}
if (visited.contains(root.c)) {
return false;
}
visited.add(root.c);
boolean res = cycle(root.left, visited)&& cycle(root.right, visited);
//没有这一行就会错,为啥??????????
visited.remove(root.c);
return res;
}