C++ not good solution but AC


  • 1
    class Solution {
        vector<TreeNode*> Cadidates;
    public:
        bool isSubtree(TreeNode* s, TreeNode* t) {
            findTNode(s, t);
            if(Cadidates.size()==0) return false;
            vector<int> nodeT;
            print(t, nodeT);
            for(int i=0; i<Cadidates.size(); i++)
            {
                vector<int> nodeI;
                print(Cadidates[i], nodeI);
                if(nodeI == nodeT) return true;
            }
            return false;
        }
        void print(TreeNode* root, vector<int>& res)
        {
            if(!root) return;
            print(root->left, res);
            res.push_back(root->val);
            print(root->right, res);
        }
        void findTNode(TreeNode* s, TreeNode * t)
        {
            if(!s) return;
            if(s->val==t->val)
                Cadidates.push_back(s);
            findTNode(s->left,t);
            findTNode(s->right, t);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.