Oh Java, thou beauty!

  • 10
    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

    almost same as the Symmetric Tree.

  • 2

    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.