題目連結

DFS 練習好題!

AC Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
#ifdef LOCAL
#include <bits/stdc++.h>
using namespace std;

// tree node stuff here...
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
#endif

static int __initialSetup = []()
{
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}
();

// handle special cases first
// [], "", ...
// range of input?
class Solution
{
private:
    // bottom up
    int ans = 0;
    int dfs(TreeNode *root)
    {
        if (root == NULL)
            return 0;

        int l = dfs(root->left);
        int r = dfs(root->right);

        int mx = 1;
        if (root->left && root->left->val == root->val + 1)
            mx = max(mx, l + 1);
        if (root->right && root->right->val == root->val + 1)
            mx = max(mx, r + 1);

        ans = max(mx, ans);

        return mx;
    }

    // top down
    int dfs(TreeNode *root, TreeNode *parent, int mx)
    {
        if (root == NULL)
            return mx;

        if (parent != nullptr && root->val == parent->val + 1)
            mx++; // extent the value from parent
        else
            mx = 1; // reset mx value to self only
        return max(mx, max(dfs(root->left, root, mx), dfs(root->right, root, mx)));
    }

public:
    int longestConsecutive(TreeNode *root)
    {
        // bottom up
        // ans = 0;
        // dfs(root);
        // return ans;

        // top down
        return dfs(root, nullptr, 0);

        // WA:
        // You can't do it directly!
        // the case below, node 4 will ret 4 to 3, and then GG
        /*
        1
         \
          3
           \
            4
           / \
          9   5
        (ret = 4)
        */
        // if (root == NULL)
        //     return 0;

        // int mx = 1;
        // if (root->left) {
        //     int ret = longestConsecutive(root->left);
        //     mx = max(mx, ret + (root->left->val == root->val + 1 ? 1 : 0));
        // }

        // if (root->right) {
        //     int ret = longestConsecutive(root->right);
        //     mx = max(mx, ret + (root->right->val == root->val + 1 ? 1 : 0));
        // }

        // return mx;
    }
};

#ifdef LOCAL
int main()
{
    return 0;
}
#endif