This approach involves using a recursive function to traverse the root tree and checking for a matching subtree starting from every node. A helper function can be used to check if trees rooted at specific nodes are identical.
Time Complexity: O(N*M), where N is the number of nodes in the root tree and M is the number of nodes in subRoot tree.
Space Complexity: O(min(N, M)), depending on the tree height during recursion.
1var isSubtree = function(root, subRoot) {
2 if (!root) return false;
3 if (isIdentical(root, subRoot)) return true;
4 return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
5};
6
7var isIdentical = function(s, t) {
8 if (!s && !t) return true;
9 if (!s || !t) return false;
10 return s.val === t.val && isIdentical(s.left, t.left) && isIdentical(s.right, t.right);
11};
The JavaScript solution is similar in logic to other solutions, employing two functions: isSubtree
and isIdentical
. It checks recursively whether the trees rooted at two nodes are the same.
Another approach to solving this problem is by converting the trees into their preorder traversal strings with marker symbols for nulls, and then checking if the subRoot string is a substring of the root string.
Time Complexity: O(N + M + N*M), where N and M are the sizes of the trees.
Space Complexity: O(N + M), for the preorder traversal strings.
1#include <string>
2#include <sstream>
3
4struct TreeNode {
5 int val;
6 TreeNode *left, *right;
7};
8
9void getPreorder(TreeNode* node, std::ostringstream& out) {
10 if (node == NULL) {
11 out << "null ";
12 return;
13 }
14 out << node->val << " ";
15 getPreorder(node->left, out);
16 getPreorder(node->right, out);
17}
18
19bool isSubtree(TreeNode* root, TreeNode* subRoot) {
20 std::ostringstream rootStream, subRootStream;
21 getPreorder(root, rootStream);
22 getPreorder(subRoot, subRootStream);
23 return rootStream.str().find(subRootStream.str()) != std::string::npos;
24}
Using ostringstream
to accumulate the preorder traversal string, this method checks if serialized subRoot is a substring of the serialized root.