572. Subtree of Another Tree
Tags: ‘Tree’
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself.
Example 1:
Given tree s:
3 / \ 4 5 / \ 1 2Given tree t:
4 / \ 1 2Return true, because t has the same structure and node values with a subtree of s.
Example 2:
Given tree s:
3 / \ 4 5 / \ 1 2 / 0Given tree t:
4 / \ 1 2Return false.
Solution
// Refer to 100-Same Tree
// check sameTree of t against every node in s
// 92% 97%
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null && t == null) return false;
else if (s != null && t == null) return true;
else if (s == null && t != null) return false;
// preorder traversal
return sameTree(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);
}
private boolean sameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if ((p == null || q == null) || p.val != q.val) return false;
return sameTree(p.left, q.left) && sameTree(p.right, q.right);
}
}
// Iterative preorder traversal
// 9% 8%
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null && t == null) return false;
else if (s != null && t == null) return true;
else if (s == null && t != null) return false;
Stack<TreeNode> stack = new Stack<>();
stack.push(s);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (sameTree(node, t)) return true;
if (node != null) {
stack.push(node.right);
stack.push(node.left);
}
}
return false;
}
private boolean sameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if ((p == null || q == null) || p.val != q.val) return false;
return sameTree(p.left, q.left) && sameTree(p.right, q.right);
}
}