leetcode算法题--二叉树中的伪回文路径
原题链接:https://leetcode-cn.com/problems/pseudo-palindromic-paths-in-a-binary-tree/
这题的技巧是判断奇偶,如一条路径为【2,3,3】,那么【3,2,3】即是回文序列。但如果一条路径为【2,3,3,4】,则无回文序列。统计每个数出现的次数,若次数为奇数的数个数大于1个,无法转换成回文序列,反之则可以。
递归
int pseudoPalindromicPaths (TreeNode* root) {int ans=0;vector<int> count(10,0);return func(root,count);
}
int func(TreeNode* root,vector<int>& count){int ans=0;count[root->val]++;if(root->left){ans+=func(root->left,count);} if(root->right){ans+=func(root->right,count);} if(!root->left&&!root->right){int tmp=0;for(int i=1;i<=9;i++){if(count[i]%2) tmp++;//统计奇数个数的数的次数}if(tmp<=1) ans++;//次数小于2,则可以转换称回文序列}count[root->val]--;return ans;
}
改进,用lambda表达式,可调用对象function
int pseudoPalindromicPaths (TreeNode* root) {int ans=0;vector<int> count(10,0);function<int(TreeNode *root,vector<int> &count)> func=[&func](TreeNode *root,vector<int> &count)->int{int ans=0;count[root->val]++;if(root->left){ans+=func(root->left,count);} if(root->right){ans+=func(root->right,count);} if(!root->left&&!root->right){int tmp=0;for(int i=1;i<=9;i++){if(count[i]%2) tmp++;//统计奇数个数的数的次数}if(tmp<=1) ans++;//次数小于2,则可以转换称回文序列}count[root->val]--;return ans;};return func(root,count);
}
leetcode算法题--二叉树中的伪回文路径相关推荐
- LeetCode 1457. 二叉树中的伪回文路径(位运算+递归)
1. 题目 给你一棵二叉树,每个节点的值为 1 到 9 .我们称二叉树中的一条路径是 「伪回文」的,当它满足:路径经过的所有节点值的排列中,存在一个回文序列. 请你返回从根到叶子节点的所有路径中 伪回 ...
- 【树】B032_LC_ 二叉树中的伪回文路径(暴力 / 优化)
一.Problem Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is ...
- leetcode算法题--二叉树中的最长交错路径★
原题链接:https://leetcode-cn.com/problems/longest-zigzag-path-in-a-binary-tree/ 嵌套递归(超时) 相关题目:二叉树中的列表 in ...
- leetcode算法题--二叉树中序遍历迭代法
原题链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 二叉树中序遍历迭代法,栈实现 vector<int> ...
- python【力扣LeetCode算法题库】409-最长回文串(数学 计数器)
最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字 ...
- python【力扣LeetCode算法题库】5- 最长回文子串
5. 最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab&quo ...
- leetcode算法题--二叉树中的列表★
原题链接:https://leetcode-cn.com/problems/linked-list-in-binary-tree/ 嵌套递归 bool isSubPath(ListNode* head ...
- leetcode算法题--矩阵中的幸运数
原题链接:https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/ class Solution {public:vector<in ...
- leetcode算法题--地图中的最高点
原题链接:https://leetcode-cn.com/problems/map-of-highest-peak/ 多源bfs class Solution {public:vector<ve ...
最新文章
- centos下docker1.7 上传文件到容器报错 Error: Path not specified
- (转)rlwrap真是一个好东西
- struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Jsp页面
- 数据流图怎么画_概率图模型怎么画?5个步骤完成专业模型图
- 爬虫-09-get请求发起-响应对象的属性了解-练习图片的爬取
- pytorch扩展——如何自定义前向和后向传播
- 向前的快捷键_平面设计基础知识:平面设计师应该知道的快捷键。
- docker portainer_Docker可视化管理:Portainer中文版
- php基本语法的几点备忘
- Devexpress ASP.NET最新版开发.NET环境配置Visual Studo和SQL Server对应版本
- springboot从OSS下载图片并打包为压缩包下载
- HTML5的最简单模板
- 有得必有失,你该把技术做多细?
- PTA 1027 Colors in Mars(读懂题目意思很重要!!)
- Python程序员的自我修养
- QT项目五:简易计算器
- CHK文件丢失怎么办?chk文件恢复技巧就看这一个!
- 计算机项目(毕设课设) 之 含文档+PPT+源码等]精品基于PHP实现的高校兼职应聘招聘系统
- 超声波明渠流量计应用于水位测量
- 上位机软件用什么写的_为什么这么多Java开发者会觉得用Java写网站很难?
热门文章
- python培训班价格-Python培训班一般多少钱?学费多少?
- python在读写文件之前需要创建文件对象-Python对象序列化写入文件对象
- python对于办公有什么帮助-日常工作中python能够有哪些帮助?
- python中文读音ndarray-numpy中的ndarray方法和属性
- python写错了怎么更改-Python中修改字符串的四种方法
- python3下载教程-《Python3从入门到放弃》视频教程
- GitHub上拥有4万+star的大佬大厂求职经验分享
- 关于深度学习中GPU显存使用的介绍
- 成都理工大学计算机报告,[2017年整理]成都理工大学通信工程计算机网络综合课程设计报告.doc...
- 身份证校验原理和PHP实现