-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0100_same_tree.kt
More file actions
21 lines (20 loc) · 820 Bytes
/
0100_same_tree.kt
File metadata and controls
21 lines (20 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// https://leetcode.com/problems/same-tree/description/
// Time complexity: O(min(p, q)) where p and q are the amount of nodes in the respective trees. Best case should be O(n)
// Space complexity: O(max(heightP, heightQ)) where the variables are the heights of their respective trees. Best case is O(logN) for a balanced tree.
/**
* 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 {
fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean {
if (p == null && q == null) return true;
if (p == null || q == null) return false;
return p.`val` == q.`val` && isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
}
}