Oh Java, thou beauty!


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

    }


  • 0
    L

    almost same as the Symmetric Tree.


  • 2
    R

    Can be improved.

    If both p and q are the same object, then you need not go through each value.

    Suggested change: first if can be

           if (p == q), instead of if(p==null && q==null)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.