Leetcode - 100. Same Tree
Leetcode - 100. Same Tree
Given the roots of two binary trees p and q, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func isSameTree(p *TreeNode, q *TreeNode) bool {
if p == nil && q == nil{
return true
}
if p==nil || q==nil{
return false
}
if p.Val != q.Val{
return false
}
left := isSameTree(p.Left, q.Left)
right := isSameTree(p.Right, q.Right)
return left && right
}
This post is licensed under CC BY 4.0 by the author.