LeetCode 865. Smallest Subtree with all the Deepest Nodes

原题链接在这里:https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/

题目:

Given a binary tree rooted at root, the depth of each node is the shortest distance to the root.

A node is deepest if it has the largest depth possible among any node in the entire tree.

The subtree of a node is that node, plus the set of all descendants of that node.

Return the node with the largest depth such that it contains all the deepest nodes in its subtree.

Example 1:

Input: [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]
Explanation:



We return the node with value 2, colored in yellow in the diagram.
The nodes colored in blue are the deepest nodes of the tree.
The input "[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]" is a serialization of the given tree.
The output "[2, 7, 4]" is a serialization of the subtree rooted at the node with value 2.
Both the input and output have TreeNode type.

Note:

  • The number of nodes in the tree will be between 1 and 500.
  • The values of each node are unique.

题解:

If both sides have deepest nodes, return root. If only left side has deepest nodes, return left side result. Vice versa.

To check if one side has deepest nodes, calculate the deepest depth of that side.

If left deepest depth == right deepest depth, then both sides have deepest nodes, return root.

If left deepest depth > right deepest depth, then only left side has deepest node, return the result node from left side.

Time Complexity: O(n).

Space: O(h). stack space.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 import javafx.util.Pair;
11 
12 class Solution {
13     public TreeNode subtreeWithAllDeepest(TreeNode root) {
14         Pair<Integer, TreeNode> res = dfs(root);
15         return res.getValue();
16     }
17     
18     private Pair<Integer, TreeNode> dfs(TreeNode root){
19         if(root == null){
20             return new Pair(0, null);
21         }
22         
23         Pair<Integer, TreeNode> left = dfs(root.left);
24         Pair<Integer, TreeNode> right = dfs(root.right);
25         
26         int lDepth = left.getKey();
27         int rDepth = right.getKey();
28         int deepestDepth = Math.max(lDepth, rDepth)+1;
29         if(lDepth < rDepth){
30             return new Pair(deepestDepth, right.getValue());
31         }else if(lDepth > rDepth){
32             return new Pair(deepestDepth, left.getValue());
33         }else{
34             return new Pair(deepestDepth, root);
35         }
36     }
37         
38 }

AC C++:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 8  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 9  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10  * };
11  */
12 class Solution {
13 public:
14     TreeNode* subtreeWithAllDeepest(TreeNode* root) {
15         return dfs(root).second;
16     }
17 
18     pair<int, TreeNode*> dfs(TreeNode* root){
19         if(!root){
20             return {0, NULL};
21         }
22 
23         pair<int, TreeNode*> left = dfs(root->left);
24         pair<int, TreeNode*> right =dfs(root->right);
25         int d1 = left.first;
26         int d2 = right.first;
27         int depth = max(d1, d2) + 1;
28         if(d1 > d2){
29             return {depth, left.second};
30         }else if(d1 < d2){
31             return {depth, right.second};
32         }else{
33             return {depth, root};
34         }
35     }
36 };

类似Lowest Common Ancestor of a Binary Tree.

posted @ 2019-07-08 13:06  Dylan_Java_NYC  阅读(394)  评论(0编辑  收藏  举报