public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null) return false;
if (isSameTree(s, t)) return true;
return isSubtree(s.left, t) || isSubtree(s.right, t);
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null) return false;
if (isSameTree(s, t)) return true;
return isSubtree(s.left, t) || isSubtree(s.right, t);
}
private boolean isSameTree(TreeNode p, TreeNode q) {
if (q == null && p == null) return true;
if (q == null || p == null) return false;
return p.val == q.val && isSameTree(p.left, q.left) &&
isSameTree(p.right, q.right);
}
}