Optimized Java Solution


  • 0
    S
    public boolean isSubtree(TreeNode s, TreeNode t) {
            if(s==null) return false;
            if(helper(s,t))return true;
            return isSubtree(s.left, t)||isSubtree(s.right, t);
        }
        
        public boolean helper(TreeNode s, TreeNode t){
                if(s == null && t == null)
                    return true;
                
                if(s == null || t == null)
                    return false;
    
                return s.val == t.val && helper(s.left, t.left) && helper(s.right, t.right);
        }
    

Log in to reply
 

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