Super simple and fast Java solution


  • 0
    E
    public class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            return toStr(p).equals(toStr(q));
        }
        
        private String toStr(TreeNode node) {
            if(node == null) return "";
            else return "(" + toStr(node.left) + "," + node.val + "," + toStr(node.right) + ")";
        }
    }

Log in to reply
 

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