1. 程式人生 > 實用技巧 >[LeetCode] 865. Smallest Subtree with all the Deepest Nodes

[LeetCode] 865. Smallest Subtree with all the Deepest Nodes

Given therootof a binary tree, the depth of each node isthe shortest distance to the root.

Returnthe smallest subtreesuch that it containsall the deepest nodesin the original tree.

A node is calledthedeepestif it has the largest depth possible amongany node in the entire tree.

Thesubtreeof a node is tree consisting of that node, plus the set of all descendants of that node.

Note:This question is the same as 1123:https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/

Example 1:

Input: root = [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 coloured in blue are the deepest nodes of the tree.
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.

Example 2:

Input: root = [1]
Output: [1]
Explanation: The root is the deepest node in the tree.

Example 3:

Input: root = [0,1,3,null,2]
Output: [2]
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.

Constraints:

  • The number of nodes in the tree will be in the range[1, 500].
  • 0 <= Node.val <= 500
  • The values of the nodes in the treeareunique.

具有所有最深節點的最小子樹。

給定一個根為root的二叉樹,每個節點的深度是 該節點到根的最短距離 。

如果一個節點在 整個樹 的任意節點之間具有最大的深度,則該節點是 最深的 。

一個節點的 子樹 是該節點加上它的所有後代的集合。

返回能滿足 以該節點為根的子樹中包含所有最深的節點 這一條件的具有最大深度的節點。

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/smallest-subtree-with-all-the-deepest-nodes
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

這道題的思路類似236題,思路是DFS + memorization,這裡我先提供一個遍歷兩次的思路。既然找的是一棵最小的子樹,但是這棵子樹包含一個深度最大的節點(也就是距離根節點最遠的節點),那麼首先我們要做的就是記錄下樹中每個節點的深度,這裡我選擇用hashmap記錄。第二次遍歷的時候我們用dfs分別去看左子樹和右子樹,看哪個子樹上返回的深度更大,則遞迴去看那個子樹。

時間O(n)

空間O(n)

Java實現

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     public TreeNode subtreeWithAllDeepest(TreeNode root) {
18         // corner case
19         if (root == null) {
20             return null;
21         }
22         // normal case
23         HashMap<TreeNode, Integer> map = new HashMap<>();
24         depth(root, map);
25         return dfs(root, map);
26     }
27 
28     private int depth(TreeNode root, HashMap<TreeNode, Integer> map) {
29         if (root == null) {
30             return 0;
31         }
32         if (map.containsKey(root)) {
33             return map.get(root);
34         }
35         int max = Math.max(depth(root.left, map), depth(root.right, map)) + 1;
36         map.put(root, max);
37         return max;
38     }
39 
40     private TreeNode dfs(TreeNode root, HashMap<TreeNode, Integer> map) {
41         int left = depth(root.left, map);
42         int right = depth(root.right, map);
43         if (left == right) {
44             return root;
45         }
46         if (left > right) {
47             return dfs(root.left, map);
48         } else {
49             return dfs(root.right, map);
50         }
51     }
52 }

相關題目

235. Lowest Common Ancestor of a Binary Search Tree

236. Lowest Common Ancestor of a Binary Tree

865. Smallest Subtree with all the Deepest Nodes

LeetCode 題目總結