Share my JAVA solution

  • 2
     public boolean isSameTree(TreeNode p, TreeNode q) {
        // base case: when one node is null, then it means it's the bottom of that branch. Now stop and check if the same node from the other tree is also null
        if (p == null || q == null) {
            return (q == null) && (p == null);
        }else {
            // check if the value is equal
            if (p.val != q.val) {
                return false;
            }else {
                // recursion
                return isSameTree(p.right,q.right) && isSameTree(p.left,q.left);

Log in to reply

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