LeetCode Entry

100. Same Tree

10.01.2023 easy 2023 kotlin

fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean = p == null && q == null ||

100. Same Tree easy

https://t.me/leetcode_daily_unstoppable/81

blog post

fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean =  p == null && q == null ||
            p?.`val` == q?.`val` && isSameTree(p?.left, q?.left) && isSameTree(p?.right, q?.right)

Check for the current node and repeat for the children. Let’s write one-liner

Space: O(logN) for stack, Time: O(n)