题目
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
解题思路
递归判断
代码
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSameTree(p *TreeNode, q *TreeNode) bool {
if nil == p && nil == q {
return true
} else if nil == p {
return false
} else if nil == q {
return false
}
if p.Val != q.Val {
return false
}
if false == isSameTree(p.Left, q.Left) {
return false
}
if false == isSameTree(p.Right, q.Right) {
return false
}
return true
}