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
No.1
Given tree s:
1 | 3 |
Given tree t:
1 | 4 |
Return true, because t has the same structure and node values with a subtree of s.
No.2
Given tree s:
1 | 3 |
Given tree t:
1 | 4 |
Return false.
Code
1 | public class TreeNode { |
1 | public boolean isSubtree(TreeNode s, TreeNode t) { |