LeetCode 题解工作台
判断 DFS 字符串是否是回文串
给你一棵 n 个节点的树,树的根节点为 0 , n 个节点的编号为 0 到 n - 1 。这棵树用一个长度为 n 的数组 parent 表示,其中 parent[i] 是节点 i 的父节点。由于节点 0 是根节点,所以 parent[0] == -1 。 给你一个长度为 n 的字符串 s ,其中 s…
6
题型
4
代码语言
3
相关题
当前训练重点
困难 · 数组·哈希·扫描
答案摘要
我们可以使用深度优先搜索(DFS)来遍历树,将整棵树的 求出来,顺便求出每个节点的区间 $[l, r]$。 然后我们使用字符串哈希的方法,分别求出 和 的逆序串的哈希值,判断是否是回文串。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路
题目描述
给你一棵 n 个节点的树,树的根节点为 0 ,n 个节点的编号为 0 到 n - 1 。这棵树用一个长度为 n 的数组 parent 表示,其中 parent[i] 是节点 i 的父节点。由于节点 0 是根节点,所以 parent[0] == -1 。
给你一个长度为 n 的字符串 s ,其中 s[i] 是节点 i 对应的字符。
一开始你有一个空字符串 dfsStr ,定义一个递归函数 dfs(int x) ,它的输入是节点 x ,并依次执行以下操作:
- 按照 节点编号升序 遍历
x的所有孩子节点y,并调用dfs(y)。 - 将 字符
s[x]添加到字符串dfsStr的末尾。
注意,所有递归函数 dfs 都共享全局变量 dfsStr 。
你需要求出一个长度为 n 的布尔数组 answer ,对于 0 到 n - 1 的每一个下标 i ,你需要执行以下操作:
- 清空字符串
dfsStr并调用dfs(i)。 - 如果结果字符串
dfsStr是一个 回文串 ,answer[i]为true,否则answer[i]为false。
请你返回字符串 answer 。
示例 1:

输入:parent = [-1,0,0,1,1,2], s = "aababa"
输出:[true,true,false,true,true,true]
解释:
- 调用
dfs(0),得到字符串dfsStr = "abaaba",是一个回文串。 - 调用
dfs(1),得到字符串dfsStr = "aba",是一个回文串。 - 调用
dfs(2),得到字符串dfsStr = "ab",不 是回文串。 - 调用
dfs(3),得到字符串dfsStr = "a",是一个回文串。 - 调用
dfs(4),得到字符串dfsStr = "b",是一个回文串。 - 调用
dfs(5),得到字符串dfsStr = "a",是一个回文串。
示例 2:

输入:parent = [-1,0,0,0,0], s = "aabcb"
输出:[true,true,true,true,true]
解释:
每一次调用 dfs(x) 都得到一个回文串。
提示:
n == parent.length == s.length1 <= n <= 105- 对于所有
i >= 1,都有0 <= parent[i] <= n - 1。 parent[0] == -1parent表示一棵合法的树。s只包含小写英文字母。
解题思路
方法一:DFS + 字符串哈希
我们可以使用深度优先搜索(DFS)来遍历树,将整棵树的 求出来,顺便求出每个节点的区间 。
然后我们使用字符串哈希的方法,分别求出 和 的逆序串的哈希值,判断是否是回文串。
时间复杂度 ,空间复杂度 。其中 为字符串 的长度。
class Hashing:
__slots__ = ["mod", "h", "p"]
def __init__(self, s: List[str], base: int, mod: int):
self.mod = mod
self.h = [0] * (len(s) + 1)
self.p = [1] * (len(s) + 1)
for i in range(1, len(s) + 1):
self.h[i] = (self.h[i - 1] * base + ord(s[i - 1])) % mod
self.p[i] = (self.p[i - 1] * base) % mod
def query(self, l: int, r: int) -> int:
return (self.h[r] - self.h[l - 1] * self.p[r - l + 1]) % self.mod
class Solution:
def findAnswer(self, parent: List[int], s: str) -> List[bool]:
def dfs(i: int):
l = len(dfsStr) + 1
for j in g[i]:
dfs(j)
dfsStr.append(s[i])
r = len(dfsStr)
pos[i] = (l, r)
n = len(s)
g = [[] for _ in range(n)]
for i in range(1, n):
g[parent[i]].append(i)
dfsStr = []
pos = {}
dfs(0)
base, mod = 13331, 998244353
h1 = Hashing(dfsStr, base, mod)
h2 = Hashing(dfsStr[::-1], base, mod)
ans = []
for i in range(n):
l, r = pos[i]
k = r - l + 1
v1 = h1.query(l, l + k // 2 - 1)
v2 = h2.query(n - r + 1, n - r + 1 + k // 2 - 1)
ans.append(v1 == v2)
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n) because each node is visited once and child accesses are O(1). Space complexity is O(n) for storing children arrays and maintaining the bitmask or frequency counts during DFS. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidate attempts naive string concatenation for each DFS call.
- question_mark
Candidate uses a hash or frequency map to track characters.
- question_mark
Candidate considers bit manipulation to optimize palindrome checking.
常见陷阱
外企场景- error
Rebuilding the DFS string for every node, leading to O(n^2) time complexity.
- error
Incorrectly handling the root node or parent array indexing.
- error
Failing to consider that a palindrome allows at most one character with odd count.
进阶变体
外企场景- arrow_right_alt
Check if BFS strings are palindromes using a similar hash counting approach.
- arrow_right_alt
Verify palindrome paths in a weighted tree with additional constraints.
- arrow_right_alt
Determine all nodes where concatenated ancestor labels form palindromes under dynamic updates.