C++ DFS


  • 0
    B
    class Solution {
    public:
        string _first = ",", _second = ",";
        
        bool isSubtree(TreeNode* s, TreeNode* t) {
            if (s == 0 && t == 0) return true;
            if (s == 0 || t == 0) return false;
            dfs(s, _first);
            dfs(t, _second);
            if (_first.find(_second) != string::npos) return true;
            return false;
        }
        void dfs(TreeNode* n, string &s) {
            if (n == 0) {s += '#,'; return;}
            s += to_string(n->val) + ',';
            dfs(n->left, s);
            dfs(n->right, s); 
        }
    };

Log in to reply
 

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