LeetCode 865. Smallest Subtree With All the Deepest Nodes

Question

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
2
3
4
5
6
7
8
Input: [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]

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.

Solution

  • use preorder and hashmap to maintain the relationship between node and depth and keep the max depth vairable. And then use the similar postorder showed in LCA
  • Or, we can build the class (which contains node and depth) and to use postorder to solve.
    • if (depth(left) > depth(right)) return new data(left.node, depth + 1);
    • if (depth(left) < depth(right)) return new data(right.node, depth + 1);
    • Otherwise, we return root, also maintain the depth. (In this case, it shows that it have deepest tree which left depth equals right depth)

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// two pass, time:O(n) space:O(n)
HashMap<TreeNode, Integer> map;
int max;
public TreeNode subtreeWithAllDeepest(TreeNode root) {
if (root == null) return null;
map = new HashMap<>();
map.put(null, -1);
dfs(root, null);
for (int d : map.values()) {
max = Math.max(max, d);
}
return findMaxDepthNode(root);
}

public void dfs(TreeNode root, TreeNode parent) {
if (root == null) return;
map.put(root, map.get(parent) + 1);
dfs(root.left, root);
dfs(root.right, root);
}

public TreeNode findMaxDepthNode(TreeNode root) {
if (root == null) return null;
if (map.get(root) == max) return root;
TreeNode left = findMaxDepthNode(root.left);
TreeNode right = findMaxDepthNode(root.right);
if (left != null && right != null) return root;
if (left != null) return left;
if (right != null) return right;
return null;
}

public class Data {
TreeNode node;
int depth;

public Data(TreeNode r, int d) {
node = r;
depth = d;
}
}

// one pass time:O(n) space:O(n)
public TreeNode subtreeWithAllDeepest2(TreeNode root) {
return dfs(root).node;
}

public Data dfs(TreeNode root) {
if (root == null) return new Data(null, 0);
Data left = dfs(root.left);
Data right = dfs(root.right);

if (left.depth > right.depth) return new Data(left.node, left.depth + 1);
if (left.depth < right.depth) return new Data(right.node, right.depth + 1);
// if it has same depth, we return root
return new Data(root, left.depth + 1);
}

Reference