simple c++ inorder traversal


  • 0
    B
        vector<int> findMode(TreeNode *root) {
            vector<int> res;
            int nNumElements(0), nRepeat(0);
            TreeNode *last = nullptr;
            function<void(TreeNode *)> walk = [&](TreeNode *node) {
                if (node) {
                    walk(node->left);
                    nRepeat = (last and last->val == node->val) ? nRepeat + 1 : 0;
                    if (nRepeat > nNumElements) {
                        res = {node->val};
                        nNumElements = nRepeat;
                    } else if(nRepeat == nNumElements) {
                        res.push_back(node->val);
                    }
                    last = node;
                    walk(node->right);
                }
            };
            walk(root);
            return res;
        }```

Log in to reply
 

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