Check if DFS Strings Are Palindromes - Problem
Tree DFS Palindrome Checker

You're given a tree with n nodes (numbered 0 to n-1) rooted at node 0, represented by a parent array where parent[i] is the parent of node i (with parent[0] = -1 for the root). Each node has a character assigned from string s.

Your task is to perform a special DFS traversal starting from each node and determine if the resulting string is a palindrome.

DFS Rules:
1. Visit children in increasing order of their node numbers
2. After visiting all children, add the current node's character to the result string
3. This creates a post-order traversal pattern

Goal: Return a boolean array where answer[i] is true if the DFS string starting from node i forms a palindrome.

Example: If node 2 has children [4,5] and characters are "abcde", the DFS from node 2 would visit: 4โ†’5โ†’2, creating string based on their characters.

Input & Output

example_1.py โ€” Basic Tree
$ Input: parent = [-1,0,0,1,1], s = "aaaaa"
โ€บ Output: [true,true,true,true,true]
๐Ÿ’ก Note: All nodes have character 'a'. Since DFS from any single node produces string of only 'a' characters, all resulting strings are palindromes. For example, DFS from node 1 visits children 3,4 then node 1, producing "aaa".
example_2.py โ€” Mixed Characters
$ Input: parent = [-1,0,0,1,1], s = "aabaa"
โ€บ Output: [true,true,true,true,true]
๐Ÿ’ก Note: Even with mixed characters, the tree structure creates palindromic DFS strings. DFS from root (node 0): visits 1โ†’(3,4)โ†’1โ†’2, producing DFS string "aabaa" which is a palindrome.
example_3.py โ€” Non-palindrome Case
$ Input: parent = [-1,0,0], s = "abc"
โ€บ Output: [true,false,false]
๐Ÿ’ก Note: DFS from node 0: visits 1,2 then 0, producing "bca" (palindrome). DFS from node 1: just "b" (palindrome). DFS from node 2: just "c" (palindrome). Wait, all single chars are palindromes, so this should be [true,true,true]. Let me reconsider... Actually the first should be "bca" which is not a palindrome.

Visualization

Tap to expand
DFS Palindrome Detection in TreesPost-order traversal with rolling hash optimizationARoot (0)BNode 1ANode 2ANode 3BNode 4DFS Traversal ResultsFrom Node 0 (Root):Visit order: 3โ†’4โ†’1โ†’2โ†’0DFS String: "ABBAA"โœ“ Palindrome!From Node 1:Visit order: 3โ†’4โ†’1DFS String: "ABB"โœ— Not palindromeRolling Hash Optimization:Forward hash = Reverse hash โŸบ PalindromeNo string building required!Time ComplexityBrute Force: O(nยฒ ร— L) - build stringsOptimized: O(nยฒ) - hash comparisonBest Possible: O(nยฒ) due to tree structure
Understanding the Visualization
1
Build Family Tree
Convert the parent relationships into a tree structure where we can easily find each person's children
2
Collect Stories
For each family member, visit their descendants first (in age order), then add their own letter to create the complete story
3
Check Palindromes
Determine if each person's family story reads the same forwards and backwards using efficient hash comparison
Key Takeaway
๐ŸŽฏ Key Insight: Rolling polynomial hash allows us to detect palindromes without building actual strings, making the solution much more efficient in practice while maintaining the same theoretical complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each of n nodes, we perform DFS (O(subtree size)) and palindrome check (O(string length)). In worst case, this becomes O(nยฒ)

n
2n
โš  Quadratic Growth
Space Complexity
O(nยฒ)

We might store strings of total length O(nยฒ) in worst case, plus recursion stack O(n)

n
2n
โš  Quadratic Space

Constraints

  • 1 โ‰ค n โ‰ค 105
  • parent.length == n
  • parent[0] == -1
  • 0 โ‰ค parent[i] โ‰ค n - 1 for i โ‰  0
  • parent represents a valid tree
  • s.length == n
  • s consists of only lowercase English letters
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
26.1K Views
Medium Frequency
~25 min Avg. Time
847 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