Print them and compare two strings:)

  • 1
    public class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        return print(p).compareTo(print(q)) == 0;
    public String print(TreeNode root){
        if(root == null){return " ";}
        return ""+print(root.left)+print(root.right)+root.val;


  • 0

    I think it's a great idea.

    but is it use O(n) space and O(n) time ?

Log in to reply

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