C代码:自己写的比较拙劣、有点缺陷
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool evaluateTree(struct TreeNode* root){
int left, right;
if (root->left != NULL) {
left = evaluateTree(root->left);
}
if (root->right != NULL) {
right = evaluateTree(root->right);
}
if (root->val == 2) {
return left + right;
}
if (root->val == 3) {
return left * right;
}
return root->val;
}
C代码:官方
bool evaluateTree(struct TreeNode* root){
if (root->left == NULL || root->right == NULL) {
return root->val;
}
int left = evaluateTree(root->left);
int right = evaluateTree(root->right);
if (root->val == 2) {
return left || right;
} else {
return left && right;
}
}