【C语言Coding】第二十二天
原创
©著作权归作者所有:来自51CTO博客作者小样小响的原创作品,请联系作者获取转载授权,否则将追究法律责任
/**
* 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);
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}