mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-25 06:48:57 +08:00
update
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
"translatedContent": "<p>给你一棵树,树上有 <code>n</code> 个节点,按从 <code>0</code> 到 <code>n-1</code> 编号。树以父节点数组的形式给出,其中 <code>parent[i]</code> 是节点 <code>i</code> 的父节点。树的根节点是编号为 <code>0</code> 的节点。</p>\n\n<p>树节点的第 <em><code>k</code> </em>个祖先节点是从该节点到根节点路径上的第 <code>k</code> 个节点。</p>\n\n<p>实现 <code>TreeAncestor</code> 类:</p>\n\n<ul>\n\t<li><code>TreeAncestor(int n, int[] parent)</code> 对树和父数组中的节点数初始化对象。</li>\n\t<li><code>getKthAncestor</code><code>(int node, int k)</code> 返回节点 <code>node</code> 的第 <code>k</code> 个祖先节点。如果不存在这样的祖先节点,返回 <code>-1</code> 。</li>\n</ul>\n\n<p> </p>\n\n<p><strong>示例 1:</strong></p>\n\n<p><strong><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/06/14/1528_ex1.png\" /></strong></p>\n\n<pre>\n<strong>输入:</strong>\n[\"TreeAncestor\",\"getKthAncestor\",\"getKthAncestor\",\"getKthAncestor\"]\n[[7,[-1,0,0,1,1,2,2]],[3,1],[5,2],[6,3]]\n\n<strong>输出:</strong>\n[null,1,0,-1]\n\n<strong>解释:</strong>\nTreeAncestor treeAncestor = new TreeAncestor(7, [-1, 0, 0, 1, 1, 2, 2]);\n\ntreeAncestor.getKthAncestor(3, 1); // 返回 1 ,它是 3 的父节点\ntreeAncestor.getKthAncestor(5, 2); // 返回 0 ,它是 5 的祖父节点\ntreeAncestor.getKthAncestor(6, 3); // 返回 -1 因为不存在满足要求的祖先节点\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 <= k <= n <= 5 * 10<sup>4</sup></code></li>\n\t<li><code>parent[0] == -1</code> 表示编号为 <code>0</code> 的节点是根节点。</li>\n\t<li>对于所有的 <code>0 < i < n</code> ,<code>0 <= parent[i] < n</code> 总成立</li>\n\t<li><code>0 <= node < n</code></li>\n\t<li>至多查询 <code>5 * 10<sup>4</sup></code> 次</li>\n</ul>\n",
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Hard",
|
||||
"likes": 93,
|
||||
"likes": 95,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
@@ -167,7 +167,7 @@
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"3.9K\", \"totalSubmission\": \"12.5K\", \"totalAcceptedRaw\": 3895, \"totalSubmissionRaw\": 12465, \"acRate\": \"31.2%\"}",
|
||||
"stats": "{\"totalAccepted\": \"4K\", \"totalSubmission\": \"12.8K\", \"totalAcceptedRaw\": 4038, \"totalSubmissionRaw\": 12794, \"acRate\": \"31.6%\"}",
|
||||
"hints": [
|
||||
"The queries must be answered efficiently to avoid time limit exceeded verdict.",
|
||||
"Use sparse table (dynamic programming application) to travel the tree upwards in a fast way."
|
||||
|
||||
Reference in New Issue
Block a user