|
| 1 | +Given the root of a binary tree, the depth of each node is the shortest distance to the root. |
| 2 | + |
| 3 | +Return the smallest subtree such that it contains all the deepest nodes in the original tree. |
| 4 | + |
| 5 | +A node is called the deepest if it has the largest depth possible among any node in the entire tree. |
| 6 | + |
| 7 | +The subtree of a node is tree consisting of that node, plus the set of all descendants of that node. |
| 8 | + |
| 9 | +Note: This question is the same as 1123: https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/ |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +Example 1: |
| 14 | + |
| 15 | + |
| 16 | +Input: root = [3,5,1,6,2,0,8,null,null,7,4] |
| 17 | +Output: [2,7,4] |
| 18 | +Explanation: We return the node with value 2, colored in yellow in the diagram. |
| 19 | +The nodes coloured in blue are the deepest nodes of the tree. |
| 20 | +Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is the smallest subtree among them, so we return it. |
| 21 | +Example 2: |
| 22 | + |
| 23 | +Input: root = [1] |
| 24 | +Output: [1] |
| 25 | +Explanation: The root is the deepest node in the tree. |
| 26 | +Example 3: |
| 27 | + |
| 28 | +Input: root = [0,1,3,null,2] |
| 29 | +Output: [2] |
| 30 | +Explanation: The deepest node in the tree is 2, the valid subtrees are the subtrees of nodes 2, 1 and 0 but the subtree of node 2 is the smallest. |
| 31 | + |
| 32 | + |
| 33 | +Constraints: |
| 34 | + |
| 35 | +The number of nodes in the tree will be in the range [1, 500]. |
| 36 | +0 <= Node.val <= 500 |
| 37 | +The values of the nodes in the tree are unique. |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +/** |
| 43 | + * Definition for a binary tree node. |
| 44 | + * struct TreeNode { |
| 45 | + * int val; |
| 46 | + * TreeNode *left; |
| 47 | + * TreeNode *right; |
| 48 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 49 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 50 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 51 | + * }; |
| 52 | + */ |
| 53 | +class Solution { |
| 54 | +public: |
| 55 | + unordered_map<TreeNode*, int> mp; |
| 56 | + int max_depth; |
| 57 | + |
| 58 | + void dfs(TreeNode* root, TreeNode* parent){ |
| 59 | + if(root){ |
| 60 | + mp.insert({root, mp[parent]+1}); |
| 61 | + dfs(root->left, root); |
| 62 | + dfs(root->right, root); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + TreeNode* answer(TreeNode* root){ |
| 67 | + if(root==NULL || mp[root]==max_depth) return root; |
| 68 | + TreeNode* L=answer(root->left); |
| 69 | + TreeNode* R=answer(root->right); |
| 70 | + if(L && R) return root; |
| 71 | + if(L) return L; |
| 72 | + if(R) return R; |
| 73 | + return NULL; |
| 74 | + } |
| 75 | + |
| 76 | + TreeNode* subtreeWithAllDeepest(TreeNode* root) { |
| 77 | + mp.insert({NULL, -1}); |
| 78 | + dfs(root, NULL); |
| 79 | + max_depth=-1; |
| 80 | + for(auto it=mp.begin();it!=mp.end();it++){ |
| 81 | + max_depth=max(max_depth, it->second); |
| 82 | + } |
| 83 | + return answer(root); |
| 84 | + } |
| 85 | +}; |
0 commit comments