Concise Java


  • 0
    R
    public class Solution {
        public boolean isSubtree(TreeNode s, TreeNode t) {
            if (s == null) return false;
            return traverse(s,t) || isSubtree(s.left, t) || isSubtree(s.right, t);
        }
        private boolean traverse(TreeNode s, TreeNode t) {
            if (s == null && t == null)  return true;
            else if (s == null || t == null) return false;
            return s.val == t.val && traverse(s.left, t.left) && traverse(s.right, t.right);
        }
    }
    

Log in to reply
 

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