题解 | #牛群的相似结构#
牛群的相似结构
https://www.nowcoder.com/practice/ecaeef0d218440d295d9eff63fbc747c?tpId=354&tqId=10591473&ru=/exam/oj/ta&qru=/ta/interview-202-top/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D354
package main
import . "nc_tools"
/*
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param p TreeNode类
* @param q TreeNode类
* @return bool布尔型
*/
func isSameTree( p *TreeNode , q *TreeNode ) bool {
// write code here
if p == nil && q == nil{
return true
}
if p ==nil || q ==nil{
return false
}
if p.Val == q.Val{
return isSameTree(p.Left, q.Left)&&isSameTree(p.Right, q.Right)
}
return false
}
知识点:树的遍历
解题思路:
两颗树同时进行先序遍历,判断当前节点是否相同,是则继续向下遍历,反之false

