50248. Evaluate a Boolean Expression Tree
難度:3.5/5 Used Time: 7:071
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#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "evaluateTree.h"
 
int solve(Node* root, int *values){
    if(root->val[0] >= 'A' && root->val[0] <= 'Z'){
        return values[root->val[0] - 'A'];
    }
    else if(root->val[0] == '!'){
        return !(solve(root->left, values));
    }
    else if(root->val[0] == '&'){
        return (solve(root->left, values) && solve(root->right, values));
    }
    else if(root->val[0] == '|'){
        return (solve(root->left, values) || solve(root->right, values));
    }
    return 0;
}
 
void evaluateTree(Node *root, int *values){
    int rtv = solve(root, values);
    #ifdef UPPER
    if(rtv) printf("TRUE\n");
    else printf("FALSE\n");
    #endif
    #ifdef LOWER
    if(rtv) printf("true\n");
    else printf("false\n");
    #endif
}