Title:
有两个不同大小的二进制树: T1
有上百万的节点; T2
有好几百的节点。请设计一种算法,判定 T2
是否为 T1
的子树
class Solution { public: /** * @param T1, T2: The roots of binary tree. * @return: True if T2 is a subtree of T1, or false. */ bool isSubtree(TreeNode *T1, TreeNode *T2) { bool result = false; if (T2 == nullptr) { return true; } if (T1 == nullptr) { return false; } // write your code here if (T1->val == T2->val) { result = dp(T1,T2); } if (!result) { result = isSubtree(T1->left,T2); } if (!result) { result = isSubtree(T1->right,T2); } return result; } bool dp (TreeNode *T1, TreeNode *T2) { if (T1 != nullptr && T2!=nullptr && T1->val == T2->val) { return dp(T1->left,T2->left) && dp (T1->right,T2->right); } if (T1 == nullptr && T2 == nullptr) { return true; } return false; } };