forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
...fter-Subtree-Removal-Queries/2458.Height-of-Binary-Tree-After-Subtree-Removal-Queries.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
Tree/2458.Height-of-Binary-Tree-After-Subtree-Removal-Queries/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.