leetcode - 1457. Pseudo-Palindromic Paths in a Binary Tree

Description

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, 10^5].
1 <= Node.val <= 9

Solution

The path is pseudo-palindromic if the frequency of each node has at most 1 odd number. So use pre-order traverse to go through all the nodes, and use a map to keep track of each distinct value’s frequency.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( n ) o(n) o(n)

Code

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int:
        val_cnt = {
   }
        stack = [(root, 0)]
        res = 0
        while stack:
            node, status = stack.pop()
            if status == 0:
                val_cnt[node.val] = val_cnt.get(node.val, 0) + 1
                stack.append((node, 1))
                if node.right:
                    stack.append((node.right, 0))
                if node.left:
                    stack.append((node.left, 0))
                # if node is a leaf
                if not node.left and not node.right:
                    # pseudo-palindromic only when at most 1 odd frequency
                    odd_cnt = 0
                    for fre_cnt in val_cnt.values():
                        if fre_cnt % 2 == 1:
                            odd_cnt += 1
                    res += (1 if odd_cnt <= 1 else 0)
            else:
                val_cnt[node.val] -= 1
        return res

相关推荐

  1. LeetCode //C - 1657. Determine if Two Strings Are Close

    2024-01-27 01:16:01       55 阅读
  2. LeetCode1547. Minimum Cost to Cut a Stick——区间dp

    2024-01-27 01:16:01       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-27 01:16:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-27 01:16:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-27 01:16:01       82 阅读
  4. Python语言-面向对象

    2024-01-27 01:16:01       91 阅读

热门阅读

  1. Ubuntu环境vscode配置Log4cplus库

    2024-01-27 01:16:01       62 阅读
  2. 2024美赛数学建模F题思路代码模型

    2024-01-27 01:16:01       52 阅读
  3. this.$copyText;vue-clipboard2作用;vue-clipboard2剪切板

    2024-01-27 01:16:01       49 阅读
  4. 算法训练营Day52(动态规划13)

    2024-01-27 01:16:01       46 阅读
  5. 网络协议基础

    2024-01-27 01:16:01       44 阅读
  6. 2024-01-26 成长-复盘

    2024-01-27 01:16:01       59 阅读