为什么我的代码无法通过 leetcode 提交,而 "Run code" 中的相同测试用例通过了
Why my code can't pass the leetcode submission while the same test case in "Run code" pass it
Link 问题:653. Two Sum IV - Input is a BST
我的代码无法通过相同的测试用例提交:
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
var Begin *TreeNode=nil
func findTarget(root *TreeNode, k int) bool {
if(Begin==nil){
Begin=root
}
if(root==nil){
return false
}
if(search(root,k-root.Val)==true){
return true
}
return findTarget(root.Left,k) || findTarget(root.Right,k)
}
func search(node *TreeNode,complement int) bool{
var curr *TreeNode=Begin
for curr!=nil{
if(complement>curr.Val){
curr=curr.Right
}else if(complement<curr.Val){
curr=curr.Left
}else{
if(curr==node){
return false
}else{
return true
}
}
}
return false
}
我用c++写过、java、python,逻辑一样,都提交通过了,奇怪的是我用的是这个测试用例(我把测试用例从提交错误提醒)
" [0,-1,2,-3,null,null,4],-4 " "运行 Code" 过程接受结果,但是用相同的测试用例提交,仍然提示错误提交.
这是因为您的 Begin
是在所有内容之前声明的,它并不总是初始化为 nil
。尝试类似的东西:
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func foo(root * TreeNode, k int, Begin * TreeNode) bool {
if (Begin == nil) {
Begin = root
}
if (root == nil) {
return false
}
if search(root, k - root.Val, Begin) {
return true
}
return foo(root.Left, k, Begin) || foo(root.Right, k, Begin)
}
func findTarget(root * TreeNode, k int) bool {
var Begin * TreeNode = nil
return foo(root, k, Begin)
}
func search(node * TreeNode, complement int, Begin * TreeNode) bool {
var curr * TreeNode = Begin
for curr != nil {
if (complement > curr.Val) {
curr = curr.Right
} else if (complement < curr.Val) {
curr = curr.Left
} else {
if (curr == node) {
return false
} else {
return true
}
}
}
return false
}
Link 问题:653. Two Sum IV - Input is a BST 我的代码无法通过相同的测试用例提交:
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
var Begin *TreeNode=nil
func findTarget(root *TreeNode, k int) bool {
if(Begin==nil){
Begin=root
}
if(root==nil){
return false
}
if(search(root,k-root.Val)==true){
return true
}
return findTarget(root.Left,k) || findTarget(root.Right,k)
}
func search(node *TreeNode,complement int) bool{
var curr *TreeNode=Begin
for curr!=nil{
if(complement>curr.Val){
curr=curr.Right
}else if(complement<curr.Val){
curr=curr.Left
}else{
if(curr==node){
return false
}else{
return true
}
}
}
return false
}
我用c++写过、java、python,逻辑一样,都提交通过了,奇怪的是我用的是这个测试用例(我把测试用例从提交错误提醒) " [0,-1,2,-3,null,null,4],-4 " "运行 Code" 过程接受结果,但是用相同的测试用例提交,仍然提示错误提交.
这是因为您的 Begin
是在所有内容之前声明的,它并不总是初始化为 nil
。尝试类似的东西:
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func foo(root * TreeNode, k int, Begin * TreeNode) bool {
if (Begin == nil) {
Begin = root
}
if (root == nil) {
return false
}
if search(root, k - root.Val, Begin) {
return true
}
return foo(root.Left, k, Begin) || foo(root.Right, k, Begin)
}
func findTarget(root * TreeNode, k int) bool {
var Begin * TreeNode = nil
return foo(root, k, Begin)
}
func search(node * TreeNode, complement int, Begin * TreeNode) bool {
var curr * TreeNode = Begin
for curr != nil {
if (complement > curr.Val) {
curr = curr.Right
} else if (complement < curr.Val) {
curr = curr.Left
} else {
if (curr == node) {
return false
} else {
return true
}
}
}
return false
}