【例题1】2331. 计算布尔二叉树的值 - 力扣(LeetCode)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool evaluateTree(struct TreeNode* root){
    if(!root->left && !root->right) return root->val;
    if(root->val == 2) return evaluateTree(root->left) || evaluateTree(root->right);
    return evaluateTree(root->left) && evaluateTree(root->right);
}

【例题2】2236. 判断根结点是否等于子结点之和 - 力扣(LeetCode)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool checkTree(struct TreeNode* root){
    if(root->val == root->left->val + root->right->val) return true;
    return false;
}

【例题3】144. 二叉树的前序遍历 - 力扣(LeetCode)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
 void doLoop(int* arr,int* returnSize,struct TreeNode* root){
    if(!root) return ;
    arr[(*returnSize)++] = root->val;
    doLoop(arr,returnSize,root->left);
    doLoop(arr,returnSize,root->right);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
    int* arr = (int*)malloc(sizeof(arr)*101);
    *returnSize = 0;
    doLoop(arr,returnSize,root);
    return arr;
}

【例题4】94. 二叉树的中序遍历 - 力扣(LeetCode)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
void doLoop(int* arr,struct TreeNode* root, int* returnSize){
    if(!root) return ;
    doLoop(arr,root->left,returnSize);
    arr[(*returnSize)++] = root->val;
    doLoop(arr,root->right,returnSize);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
    int* arr = (int*)malloc(sizeof(int)*101);
    *returnSize = 0;
    doLoop(arr,root,returnSize);
    return arr;
}

【例题5】145. 二叉树的后序遍历 - 力扣(LeetCode)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
void doLoop(int* arr,struct TreeNode* root, int* returnSize){
    if(!root)return ;
    doLoop(arr,root->left,returnSize);
    doLoop(arr,root->right,returnSize);
    arr[(*returnSize)++] = root->val;
 }
int* postorderTraversal(struct TreeNode* root, int* returnSize){
    int* arr = (int*)malloc(sizeof(int)*101);
    *returnSize = 0;
    doLoop(arr,root,returnSize);
    return arr;
}