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:
Return true, because t has the same structure and node values with a subtree of s.
Example 2:
Given tree s:
Return false.
解法1:
很简单的一个递归算法。如果当前节点数值一样,那么先判断是否identical,如果不是,考虑left, 考虑right是否和target tree存在subset关系。
C++
Java