2023每日刷题(四十)
Leetcode—1457.二叉树中的伪回文路径
实现代码
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/
int record[10] = {0};int accumulate(int rec[]) {int nums = 0;for(int i = 0; i < 10; i++) {nums += rec[i];}return nums;
}int dfs(struct TreeNode* root) {if(root == NULL) {return 0;}record[root->val] ^= 1;int res = 0;// 如果该结点为叶子结点if(root->left == root->right) {res = accumulate(record);if(res <= 1) {res = 1;} else {res = 0;}} else {res = dfs(root->left) + dfs(root->right);}record[root->val] ^= 1;return res;
}int pseudoPalindromicPaths (struct TreeNode* root) {return dfs(root);
}
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!