Check if DFS Strings Are Palindromes - Problem
Tree DFS Palindrome Checker
You're given a tree with
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
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.
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
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ยฒ)
โ Quadratic Growth
Space Complexity
O(nยฒ)
We might store strings of total length O(nยฒ) in worst case, plus recursion stack O(n)
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code