Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 3 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
[2277.Closest-Node-to-Path-in-Tree](https://github.com/wisdompeak/LeetCode/tree/master/Tree/2277.Closest-Node-to-Path-in-Tree) (H-)
[2313.Minimum-Flips-in-Binary-Tree-to-Get-Result](https://github.com/wisdompeak/LeetCode/tree/master/Tree/2313.Minimum-Flips-in-Binary-Tree-to-Get-Result) (H)
[2322.Minimum-Score-After-Removals-on-a-Tree](https://github.com/wisdompeak/LeetCode/tree/master/Tree/2322.Minimum-Score-After-Removals-on-a-Tree) (H-)
[2458.Height-of-Binary-Tree-After-Subtree-Removal-Queries](https://github.com/wisdompeak/LeetCode/tree/master/Tree/2458.Height-of-Binary-Tree-After-Subtree-Removal-Queries) (M+)
* ``Path in a tree``
[543.Diameter-of-Binary-Tree](https://github.com/wisdompeak/LeetCode/tree/master/Tree/543.Diameter-of-Binary-Tree) (M)
[124.Binary-Tree-Maximum-Path-Sum](https://github.com/wisdompeak/LeetCode/tree/master/Tree/124.Binary-Tree-Maximum-Path-Sum) (M)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
unordered_map<int,vector<int>>d2h;
int depth[100005];
int height[100005];
public:
vector<int> treeQueries(TreeNode* root, vector<int>& queries)
{
dfs_height(root, 0);
for (auto& [d, hs] : d2h)
sort(hs.rbegin(), hs.rend());

vector<int>rets;
for (int node: queries)
{
int d = depth[node];
int h = height[node];
if (d2h[d].size()==1)
rets.push_back(d - 1);
else if (d2h[d][0] == h)
rets.push_back(d2h[d][1] + d);
else
rets.push_back(d2h[d][0] + d);
}
return rets;

}

int dfs_height(TreeNode* node, int d)
{
if (node==NULL) return -1;
int h = max(dfs_height(node->left, d+1), dfs_height(node->right, d+1)) + 1;
d2h[d].push_back(h);
depth[node->val] = d;
height[node->val] = h;
return h;
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### 2458.Height-of-Binary-Tree-After-Subtree-Removal-Queries

我们定义一个节点的height表示从它到叶子节点的最大距离,depth表示从它到root的距离。

我们移除node节点对应的子树后,剩下的树的高度其实就取决于与它同depth的节点的height。所以我们将所有处在同depth的节点的height都提前收集好,那么就很容易找到其他子树的最大height。

特别注意,如果某个深度的节点只有一个,那么将其移除后,剩下树的最大高度就是该节点的depth-1.

AltStyle によって変換されたページ (->オリジナル) /