Check if DFS Strings Are Palindromes - Problem

You are given a tree rooted at node 0, consisting of n nodes numbered from 0 to n - 1. The tree is represented by an array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1.

You are also given a string s of length n, where s[i] is the character assigned to node i.

Consider an empty string dfsStr, and define a recursive function dfs(int x) that takes a node x as a parameter and performs the following steps in order:

  • Iterate over each child y of x in increasing order of their numbers, and call dfs(y).
  • Add the character s[x] to the end of the string dfsStr.

Note that dfsStr is shared across all recursive calls of dfs.

You need to find a boolean array answer of size n, where for each index i from 0 to n - 1, you do the following:

  • Empty the string dfsStr and call dfs(i).
  • If the resulting string dfsStr is a palindrome, then set answer[i] to true. Otherwise, set answer[i] to false.

Return the array answer.

Input & Output

Example 1 — Basic Tree
$ Input: parent = [-1,0,0,1,1], s = "aaaab"
Output: [true,true,true,true,true]
💡 Note: Node 0 DFS: visit 3,4,1,2,0 → "aabaa" (palindrome). Node 1 DFS: visit 3,4,1 → "aba" (palindrome). All other nodes produce single characters or palindromes.
Example 2 — Mixed Characters
$ Input: parent = [-1,0,0,1], s = "abcd"
Output: [false,false,true,true]
💡 Note: Node 0 DFS: visit 3,1,2,0 → "dcba" (not palindrome). Node 1 DFS: visit 3,1 → "db" (not palindrome). Nodes 2,3 are single characters (palindromes).
Example 3 — Single Node
$ Input: parent = [-1], s = "a"
Output: [true]
💡 Note: Only root node, DFS produces "a" which is a palindrome.

Constraints

  • n == parent.length == s.length
  • 1 ≤ n ≤ 105
  • parent[0] == -1
  • 0 ≤ parent[i] ≤ n - 1 for i ≠ 0
  • parent represents a valid tree
  • s consists of only lowercase English letters

Visualization

Tap to expand
DFS Tree Palindrome Problem Overviewparent = [-1,0,0,1,1], s = "aaaab"0a1a2a3a4bDFS from Node 0 (Post-order)1. Visit children of 0: nodes 1,22. Visit children of 1: nodes 3,43. Visit order: 3→4→1→2→04. DFS string: "a"+"b"+"a"+"a"+"a" = "aabaa"5. "aabaa" is palindrome ✓Final ResultsNode 0: "aabaa" → true (palindrome)Node 1: "aba" → true (palindrome)Nodes 2,3,4: single chars → trueOutput: [true,true,true,true,true]
Understanding the Visualization
1
Tree Structure
Build tree from parent array with node characters
2
DFS Traversal
Post-order DFS: children first, then current node
3
Palindrome Check
Verify if DFS string reads same forwards and backwards
Key Takeaway
🎯 Key Insight: DFS creates post-order strings by visiting children first, then adding current node character
Asked in
Google 25 Microsoft 18 Amazon 15
12.5K Views
Medium Frequency
~35 min Avg. Time
286 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen