LeetCode: Same Tree Solution

Recursion

Approach

Check if both node of

p
and
q
are the same, then recursively check the child nodes

Implementation

1var isSameTree = function (p, q) {
2 if (!p && !q) return true
3 if (!p || !q) return false
4 if (p.val !== q.val) return false
5 return isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
6}

References

Original problem

Comments

Loading comments...

Tags

leetcode

recursion

tree

binary tree

Next Post

CodeWars: Recover a secret string from random triplets

Sep 28, 2022

It's easy to make thing more complicated

Previous Post

LeetCode: Sort Colors

Sep 26, 2022

Count and overwrite

HoningJS

Search Posts