Easy recursive Java solution


  • 0
    A

    Easy Solution:

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

Log in to reply
 

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