Easy
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.
Example 1:
Input: p = [1,2,3], q = [1,2,3]
Output: true
Example 2:
Input: p = [1,2], q = [1,null,2]
Output: false
Example 3:
Input: p = [1,2,1], q = [1,1,2]
Output: false
Constraints:
[0, 100]
.-104 <= Node.val <= 104
import com_github_leetcode.TreeNode
/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
private fun trav(n: TreeNode?, m: TreeNode?): Boolean {
return if (n != null && m != null) {
if (n.`val` != m.`val`) {
false
} else {
trav(n.left, m.left) && trav(n.right, m.right)
}
} else {
n == null && m == null
}
}
fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean {
if (p == null && q == null) {
return true
} else if (p == null || q == null) {
return false
}
return trav(p, q)
}
}