g0001_0100.s0100_same_tree.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0100_same_tree
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Level_2_Day_15_Tree
// #Udemy_Tree_Stack_Queue #2022_09_28_Time_208_ms_(72.24%)_Space_34.3_MB_(34.28%)
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)
}
}