nodejs做网站的弊端,湖州市网站建设公司,网站的建设要多少钱,梧州论坛组织参观活动具有所有最深节点的最小子树
力扣链接#xff1a;865. 具有所有最深节点的最小子树 力扣链接#xff1a;1123. 最深叶节点的最近公共祖先
题目描述
给定一个根为 root 的二叉树#xff0c;每个节点的深度是 该节点到根的最短距离 。
返回包含原始树中所有 最深节点 的…具有所有最深节点的最小子树
力扣链接865. 具有所有最深节点的最小子树 力扣链接1123. 最深叶节点的最近公共祖先
题目描述
给定一个根为 root 的二叉树每个节点的深度是 该节点到根的最短距离 。
返回包含原始树中所有 最深节点 的 最小子树 。
如果一个节点在 整个树 的任意节点之间具有最大的深度则该节点是 最深的 。
一个节点的 子树 是该节点加上它的所有后代的集合。
示例 Java代码
class Solution {public TreeNode lcaDeepestLeaves(TreeNode root) {if(root ! null){int ldep maxDeep(root.left), rdep maxDeep(root.right);if(ldep rdep) return root;else if(ldep rdep) return lcaDeepestLeaves(root.left);else if(rdep ldep) return lcaDeepestLeaves(root.right);}return null;}public int maxDeep(TreeNode root) {if(root null) { return 0;}else {return Math.max(maxDeep(root.left), maxDeep(root.right)) 1;}}
}