Leetcode 1457. Pseudo-Palindromic Paths in a Binary Tree (二叉树遍历题)

  1. Pseudo-Palindromic Paths in a Binary Tree
    Medium
    Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node values in the path is a palindrome.

Return the number of pseudo-palindromic paths going from the root node to leaf nodes.

Example 1:

Input: root = [2,3,1,3,1,null,1]

Output: 2

Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in [3,2,3] (palindrome) and the green path [2,1,1] can be rearranged in [1,2,1] (palindrome).

Example 2:

Input: root = [2,1,1,1,3,null,null,null,null,null,1]

Output: 1

Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in [1,2,1] (palindrome).

Example 3:

Input: root = [9]

Output: 1

Constraints:

The number of nodes in the tree is in the range [1, 105].

1 <= Node.val <= 9

解法1:

注意:这里的path是指从root到leaf。

怎么才能算pseudo-palindromic呢? 只要统计出1-9每个数字的个数,如果奇数个的个数<=1就算!

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int pseudoPalindromicPaths (TreeNode* root) {
        pathNum.resize(10, 0);
        helper(root);
        return count;
    }
private:
    vector<int> pathNum;
    int count = 0;
    void helper(TreeNode *root) {
        if (!root) return;
        pathNum[root->val]++;
        if (!root->left && !root->right) {
            int oddNum = 0, evenNum = 0;
            for (int i = 1; i <= 9; i++) {
                if (pathNum[i] & 0x1) oddNum++;
                else evenNum++;
            }
            if (oddNum <= 1) {
                count++;
                //return;
            }
            pathNum[root->val]--; //记得这里也要--
            return;
        }
        helper(root->left);
        helper(root->right);
        pathNum[root->val]--;
        return;        
    }
};

解法2:思路跟上面差不多,但是用XOR。

注意:

  1. bitwise的操作优先级都很低,要加括号。比如
    (pathXor & (pathXor - 1)) == 0
    pathXor ^= (0x1 << (root->val));
  2. 要用 pathXor ^= (0x1 << (root->val)), 不能直接用pathXor ^= root->val。
    否则如果有多个数的个数是奇数的话,pathXor看不出来。
    用pathXor ^= (0x1 << (root->val))的话,1-9每个数字个数是奇数还是偶数就一目了然了。
  3. int x。 x & (x-1)会抹掉最后一个1。那么如果x=0的话,是不是也成立呢? 是的,因为0&(-1)=0.
    所以,如果pathXor有一个1,或者为全0,那么pathXor & (pathXor - 1)都是0。这个就可以作为有pseudo-palindromic的评判标准。
cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int pseudoPalindromicPaths (TreeNode* root) {
        helper(root);
        return count;
    }
private:
    int pathXor = 0;
    int count = 0;
    void helper(TreeNode *root) {
        if (!root) return;
        pathXor ^= (0x1 << (root->val));
        if (!root->left && !root->right) {
            if ((pathXor & (pathXor - 1)) == 0) count++;
            pathXor ^= (0x1 << (root->val));
            return;
        }
        helper(root->left);
        helper(root->right);
        pathXor ^= (0x1 << (root->val));
        return;        
    }
};
相关推荐
web_1553427465628 分钟前
性能巅峰对决:Rust vs C++ —— 速度、安全与权衡的艺术
c++·算法·rust
9毫米的幻想30 分钟前
【Linux系统】—— 冯诺依曼体系结构与操作系统初理解
linux·运维·服务器·c语言·c++
leoufung2 小时前
vim 多个关键字高亮插件介绍
linux·编辑器·vim
Nerd Nirvana5 小时前
软考—系统架构设计(案例 | 论文)
linux·系统架构·软件工程·软考·计算机基础
勤奋的凯尔森同学6 小时前
webmin配置终端显示样式,模仿UbuntuDesktop终端
linux·运维·服务器·ubuntu·webmin
计算机小白一个7 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^8 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
大数据追光猿10 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!10 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉10 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode