Skip to content

Latest commit

 

History

History
20 lines (19 loc) · 521 Bytes

100. Same Tree.md

File metadata and controls

20 lines (19 loc) · 521 Bytes

Solution1

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null || q == null) {
            return false;
        }
        if (p.val != q.val) {
            return false;
        }
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}

note

  • recursion. Base case: If two trees are both null, they are same. if they both exist, they should have same values.