Preorder java 5 lines


  • 0
    H
    class Solution {
        public boolean isSubtree(TreeNode s, TreeNode t) {
            return preorder(s).contains(preorder(t));
        }
        
        private String preorder(TreeNode s)
        {
            if(s == null) return ",#";
            return ","+s.val + "(" + preorder(s.left) + ")(" + preorder(s.right) + ")";
        }
    }
    

Log in to reply
 

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