我要在 Golang 中比较二叉树。但我的答案是错误的。需要第三只眼的帮助。谢谢。package mainimport( "fmt")type TreeNode struct { val int left *TreeNode right *TreeNode}func isSameTree(p *TreeNode , q *TreeNode ) (bool){ if p == nil && q == nil { return true } if p != nil && q == nil{ return false; } if p ==nil && q != nil { return false; } if (p.val == q.val) && (isSameTree(p.left,q.left)) && (isSameTree(p.right ,q.left)){ return true; } else { return false; }}func main(){ p := &TreeNode{val: 1} p.left = &TreeNode{val: 2} p.right = &TreeNode{val: 3} q := &TreeNode{val: 1} q.left = &TreeNode{val: 2} q.right = &TreeNode{val: 3} isSame := isSameTree(p,q) fmt.Println("is same?: ", isSame)}转到此代码的游乐场链接:https : //play.golang.org/p/mTX3aBxh6_
1 回答
- 1 回答
- 0 关注
- 187 浏览
添加回答
举报
0/150
提交
取消