Simple Java Solution


  • 0
    J
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        
        public boolean isSameTree(TreeNode p, TreeNode q) {
            
            TreeNode left = p;
            TreeNode right = q;
            
            if (left == null || right == null) {
                return left == right;
            }
            
            if (left.val != right.val) {
                return false;
            }
            
            return isSameTree(left.left, right.left) && isSameTree(left.right, right.right);
        }
    }

Log in to reply
 

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