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: [false,true,true,true,true]
💡 Note: Node 0 DFS: visit 3,4,1,2,0 → "abaaa" (not palindrome). Node 1 DFS: visit 3,4,1 → "aba" (palindrome). Nodes 2,3,4 are single characters or produce 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 Strings Palindrome Check INPUT Tree Structure (root=0) 0 'a' 1 'a' 2 'a' 3 'a' 4 'b' parent array: [-1, 0, 0, 1, 1] string s: "aaaab" ALGORITHM STEPS 1 Build Adjacency List Create children map from parent[] 2 Precompute DFS Strings Store dfsStr for each subtree 3 DFS Post-order Traversal Visit children, then add s[x] 4 Check Palindrome Compare with reverse DFS Strings per Node: dfs(0): "abaaaa" [OK] dfs(1): "aba" [OK] dfs(2): "a" [OK] dfs(3): "a" [OK] dfs(4): "b" [OK] FINAL RESULT Palindrome Check Results: T i=0 T i=1 T i=2 T i=3 T i=4 Output Array: [T, T, T, T, T] All subtrees form palindromes! Key Insight: Precompute DFS strings for all subtrees to avoid redundant traversals. The DFS post-order visits children in sorted order, then appends current node's character. A single character or symmetric strings like "aba" and "abaaaa" (reads same forwards/backwards) are palindromes. TutorialsPoint - Check if DFS Strings Are Palindromes | Optimized with Precomputed Strings
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