865. 具有所有最深节点的最小子树
给定一个根为 root
的二叉树,每个节点的深度是 该节点到根的最短距离 。
返回包含原始树中所有 最深节点 的 最小子树 。
如果一个节点在 整个树 的任意节点之间具有最大的深度,则该节点是 最深的 。
一个节点的 子树 是该节点加上它的所有后代的集合。
示例 1:
输入:root = [3,5,1,6,2,0,8,null,null,7,4] 输出:[2,7,4] 解释: 我们返回值为 2 的节点,在图中用黄色标记。 在图中用蓝色标记的是树的最深的节点。 注意,节点 5、3 和 2 包含树中最深的节点,但节点 2 的子树最小,因此我们返回它。
示例 2:
输入:root = [1] 输出:[1] 解释:根节点是树中最深的节点。
示例 3:
输入:root = [0,1,3,null,2] 输出:[2] 解释:树中最深的节点为 2 ,有效子树为节点 2、1 和 0 的子树,但节点 2 的子树最小。
提示:
[1, 500]
范围内。0 <= Node.val <= 500
注意:本题与力扣 1123 重复:https://leetcode.cn/problems/lowest-common-ancestor-of-deepest-leaves
原站题解
java 解法, 执行用时: 0 ms, 内存消耗: 39.5 MB, 提交时间: 2023-09-06 09:39:14
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode subtreeWithAllDeepest(TreeNode root) { return dfs(root).getValue(); } private Pair<Integer, TreeNode> dfs(TreeNode node) { if (node == null) return new Pair<>(0, null); var left = dfs(node.left); var right = dfs(node.right); if (left.getKey() > right.getKey()) // 左子树更高 return new Pair<>(left.getKey() + 1, left.getValue()); if (left.getKey() < right.getKey()) // 右子树更高 return new Pair<>(right.getKey() + 1, right.getValue()); return new Pair<>(left.getKey() + 1, node); // 一样高 } }
javascript 解法, 执行用时: 68 ms, 内存消耗: 43.3 MB, 提交时间: 2023-09-06 09:38:45
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */ /** * @param {TreeNode} root * @return {TreeNode} */ var subtreeWithAllDeepest = function(root) { return dfs(root)[1]; }; var dfs = function (node) { if (node === null) return [0, null]; const [leftHeight, leftLca] = dfs(node.left); const [rightHeight, rightLca] = dfs(node.right); if (leftHeight > rightHeight) // 左子树更高 return [leftHeight + 1, leftLca]; if (leftHeight < rightHeight) // 右子树更高 return [rightHeight + 1, rightLca]; return [leftHeight + 1, node]; // 一样高 };
golang 解法, 执行用时: 0 ms, 内存消耗: 2.7 MB, 提交时间: 2023-09-06 09:37:50
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func subtreeWithAllDeepest(root *TreeNode) *TreeNode { _, lca := dfs(root) return lca } func dfs(node *TreeNode) (int, *TreeNode) { if node == nil { return 0, nil } leftHeight, leftLCA := dfs(node.Left) rightHeight, rightLCA := dfs(node.Right) if leftHeight > rightHeight { // 左子树更高 return leftHeight + 1, leftLCA } if leftHeight < rightHeight { // 右子树更高 return rightHeight + 1, rightLCA } return leftHeight + 1, node // 一样高 }
php 解法, 执行用时: 8 ms, 内存消耗: 19.2 MB, 提交时间: 2023-09-06 09:36:01
/** * Definition for a binary tree node. * class TreeNode { * public $val = null; * public $left = null; * public $right = null; * function __construct($val = 0, $left = null, $right = null) { * $this->val = $val; * $this->left = $left; * $this->right = $right; * } * } */ class Solution { /** * @param TreeNode $root * @return TreeNode */ function subtreeWithAllDeepest($root) { return $this->dfs($root)[1]; } function dfs($node) { if ( $node == null ) return [0, null]; list($left_height, $left_lca) = $this->dfs($node->left); list($right_height, $right_lca) = $this->dfs($node->right); if ( $left_height > $right_height ) return [$left_height + 1, $left_lca]; if ( $left_height < $right_height ) return [$right_height + 1, $right_lca]; return [$left_height + 1, $node]; } }
python3 解法, 执行用时: 44 ms, 内存消耗: 16.4 MB, 提交时间: 2023-09-06 09:31:40
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def subtreeWithAllDeepest(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def dfs(node: Optional[TreeNode]) -> (int, Optional[TreeNode]): if node is None: return 0, None left_height, left_lca = dfs(node.left) right_height, right_lca = dfs(node.right) if left_height > right_height: # 左子树更高 return left_height + 1, left_lca if left_height < right_height: # 右子树更高 return right_height + 1, right_lca return left_height + 1, node # 一样高 return dfs(root)[1]
python3 解法, 执行用时: 48 ms, 内存消耗: 15.2 MB, 提交时间: 2022-11-25 11:50:47
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution(object): def subtreeWithAllDeepest(self, root): # The result of a subtree is: # Result.node: the largest depth node that is equal to or # an ancestor of all the deepest nodes of this subtree. # Result.dist: the number of nodes in the path from the root # of this subtree, to the deepest node in this subtree. Result = collections.namedtuple("Result", ("node", "dist")) def dfs(node): # Return the result of the subtree at this node. if not node: return Result(None, 0) L, R = dfs(node.left), dfs(node.right) if L.dist > R.dist: return Result(L.node, L.dist + 1) if L.dist < R.dist: return Result(R.node, R.dist + 1) return Result(node, L.dist + 1) return dfs(root).node
python3 解法, 执行用时: 44 ms, 内存消耗: 15.1 MB, 提交时间: 2022-11-25 11:50:20
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode: # Tag each node with it's depth. depth = {None: -1} def dfs(node, parent = None): if node: depth[node] = depth[parent] + 1 dfs(node.left, node) dfs(node.right, node) dfs(root) max_depth = max(depth.values()) def answer(node): # Return the answer for the subtree at node. if not node or depth.get(node, None) == max_depth: return node L, R = answer(node.left), answer(node.right) return node if L and R else L or R return answer(root)