1457. Pseudo-Palindromic Paths in a Binary Tree LeetCode Solution

In this guide, you will get 1457. Pseudo-Palindromic Paths in a Binary Tree LeetCode Solution with the best time and space complexity. The solution to Pseudo-Palindromic Paths in a Binary Tree problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Pseudo-Palindromic Paths in a Binary Tree solution in C++
  4. Pseudo-Palindromic Paths in a Binary Tree solution in Java
  5. Pseudo-Palindromic Paths in a Binary Tree solution in Python
  6. Additional Resources
1457. Pseudo-Palindromic Paths in a Binary Tree LeetCode Solution image

Problem Statement of Pseudo-Palindromic Paths in a Binary Tree

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).

See also  840. Magic Squares In Grid LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(h)

1457. Pseudo-Palindromic Paths in a Binary Tree LeetCode Solution in C++

class Solution {
 public:
  int pseudoPalindromicPaths(TreeNode* root) {
    int ans = 0;
    dfs(root, 0, ans);
    return ans;
  }

 private:
  void dfs(TreeNode* root, int path, int& ans) {
    if (root == nullptr)
      return;
    if (root->left == nullptr && root->right == nullptr) {
      path ^= 1 << root->val;
      if ((path & (path - 1)) == 0)
        ++ans;
      return;
    }

    dfs(root->left, path ^ 1 << root->val, ans);
    dfs(root->right, path ^ 1 << root->val, ans);
  }
};
/* code provided by PROGIEZ */

1457. Pseudo-Palindromic Paths in a Binary Tree LeetCode Solution in Java

class Solution {
  public int pseudoPalindromicPaths(TreeNode root) {
    dfs(root, 0);
    return ans;
  }

  private int ans = 0;

  private void dfs(TreeNode root, int path) {
    if (root == null)
      return;
    if (root.left == null && root.right == null) {
      path ^= 1 << root.val;
      if ((path & (path - 1)) == 0)
        ++ans;
      return;
    }

    dfs(root.left, path ^ 1 << root.val);
    dfs(root.right, path ^ 1 << root.val);
  }
}
// code provided by PROGIEZ

1457. Pseudo-Palindromic Paths in a Binary Tree LeetCode Solution in Python

class Solution:
  def pseudoPalindromicPaths(self, root: TreeNode | None) -> int:
    ans = 0

    def dfs(root: TreeNode | None, path: int) -> None:
      nonlocal ans
      if not root:
        return
      if not root.left and not root.right:
        path ^= 1 << root.val
        if path & (path - 1) == 0:
          ans += 1
        return

      dfs(root.left, path ^ 1 << root.val)
      dfs(root.right, path ^ 1 << root.val)

    dfs(root, 0)
    return ans
# code by PROGIEZ

Additional Resources

See also  1561. Maximum Number of Coins You Can Get LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.