Leetcode 865. Smallest Subtree with all the Deepest Nodes
Given the root of a binary tree, return the smallest subtree such that it contains all the deepest nodes in the original tree. A node is called the deepest if it has the largest depth possible among any node in the entire tree. The subtree of a node is a tree consisting of that node, plus the set of all descendants of that node.
#include <iostream>
#include <utility> // for std::pair
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
std::pair<int, TreeNode*> dfs(TreeNode* root) {
if (!root) return {0, nullptr};
auto left = dfs(root->left);
auto right = dfs(root->right);
int leftDepth = left.first;
int rightDepth = right.first;
if (leftDepth == rightDepth) {
return {leftDepth + 1, root};
} else if (leftDepth > rightDepth) {
return {leftDepth + 1, left.second};
} else {
return {rightDepth + 1, right.second};
}
}
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
return dfs(root).second;
}
};
// Example to use the solution
int main() {
// Construct a binary tree
TreeNode* root = new TreeNode(3);
root->left = new TreeNode(5);
root->right = new TreeNode(1);
root->left->left = new TreeNode(6);
root->left->right = new TreeNode(2);
root->right->left = new TreeNode(0);
root->right->right = new TreeNode(8);
root->left->right->left = new TreeNode(7);
root->left->right->right = new TreeNode(4);
// Get the solution
Solution solution;
TreeNode* result = solution.subtreeWithAllDeepest(root);
// Print the result value
if (result) {
std::cout << "The smallest subtree containing all the deepest nodes is rooted at node with value: " << result->val << std::endl;
}
// Cleaning up the dynamically allocated memory
// In a real application, you should free all allocated nodes.
return 0;
}
The provided solution ensures efficient traversal and subtree determination, keeping the complexity within acceptable bounds even for the upper limit of node count.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?