My uncommon java solution


  • 0
    T
    public class Solution {
        
        public boolean isSameTree(TreeNode p, TreeNode q) {
            
            StringBuilder pStringBuilder = new StringBuilder();
            StringBuilder qStringBuilder = new StringBuilder();
            
            walkThroughTree(p, pStringBuilder);
            walkThroughTree(q, qStringBuilder);
            
            return pStringBuilder.toString().equals(qStringBuilder.toString());
        }
        
        public TreeNode walkThroughTree(TreeNode node, StringBuilder values) {
            if (node == null) {
                values.append("null");
                return null;
            }
            
            values.append(node.val);
            
            TreeNode left = walkThroughTree(node.left, values);
            TreeNode right = walkThroughTree(node.right, values);
            
            return node;
        }
    }
    

Log in to reply
 

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