Grokking Tree Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Pseudo-Palindromic Paths in a Binary Tree (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a binary tree where each node has a value between 1 and 9, return the number of "pseudo-palindromic" paths from the root node to any leaf node.

A path is called "pseudo-palindromic" if the values along the path can be rearranged to form a palindrome.

Examples

Example 1

  • Input:
        5
       / \
      4   1
     / \   \
    4   1   1
    
  • Expected Output: 2
  • Justification: The paths are:
    • 5 → 4 → 4 (pseudo-palindromic: "5, 4, 4" can be rearranged to "4, 5, 4")
    • 5 → 1 → 1 (pseudo-palindromic: "5, 1, 1" can be rearranged to "1, 5, 1")

Example 2

  • Input:
        2
       / \
      3   1
     /   / \
    3   1   1
    
  • Expected Output: 3
  • Justification: All 3 paths are pseudo-palindromic paths.

Example 3

  • Input:
        9
       / \
      5   5
     /   / \
    5   1   7
    
  • Expected Output: 1
  • Justification: The only path 9 → 5 → 5 is a psuedo-palindromic path.

Constraints:

  • The number of nodes in the tree is in the range [1, 10<sup>5</sup>].
  • 1 <= Node.val <= 9

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible