Compare inorder serialization of two trees


  • 0
    J
    public class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            return serialize(p).equals(serialize(q));
        }
        
        String serialize(TreeNode p){
            if(p == null){
                return "#null";
            }
            String left = serialize(p.left);
            String right = serialize(p.right);
            return "#" + Integer.toString(p.val) + left + right;
        }
    }
    

Log in to reply
 

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