Smallest Subtree with all the Deepest Nodes Solutions in C++
Number 865
Difficulty Medium
Acceptance 60.8%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/description//// Author : liuyubobobo/// Time : 2018-07-07#include <iostream>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Recursion/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {private:int deepest;TreeNode* res;public:TreeNode* subtreeWithAllDeepest(TreeNode* root) {res = NULL;deepest = -1;getRes(root, 0);return res;}private:int getRes(TreeNode* node, int depth){if(node == NULL)return depth - 1;if(depth > deepest){res = node;deepest = depth;}int dl = getRes(node->left, depth + 1);int dr = getRes(node->right, depth + 1);if(dl == dr && dl == deepest)res = node;return max(dl, dr);}};int main() {cout << Solution().subtreeWithAllDeepest(new TreeNode(1))->val << endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/description//// Author : liuyubobobo/// Time : 2018-07-07#include <iostream>#include <unordered_map>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Using a Two Phase Recursion/// Which lead to a longer code, but more easy to understand:)/// Besides, this method doesn't use any class member variables:)////// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:TreeNode* subtreeWithAllDeepest(TreeNode* root) {unordered_map<TreeNode*, int> depth;getDepth(root, 0, depth);int max_depth = -1;for(const pair<TreeNode*, int>& p: depth)max_depth = max(max_depth, p.second);return getRes(root, depth, max_depth);}private:void getDepth(TreeNode* node, int d, unordered_map<TreeNode*, int>& depth){if(node == NULL)return;depth[node] = d;getDepth(node->left, d + 1, depth);getDepth(node->right, d + 1, depth);}TreeNode* getRes(TreeNode* node, unordered_map<TreeNode*, int>& depth,int max_depth){if(node == NULL)return NULL;if(depth[node] == max_depth)return node;TreeNode* L = getRes(node->left, depth, max_depth);TreeNode* R = getRes(node->right, depth, max_depth);if(L && R) return node;if(L) return L;if(R) return R;return NULL;}};int main() {cout << Solution().subtreeWithAllDeepest(new TreeNode(1))->val << endl;return 0;}