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

45 篇文章 0 订阅

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

1. 题目介绍

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.

给出一个二叉树,root 是它的根节点,每个节点的深度是指到达 root 的最短距离。
现在有两个定义:
1 最深的节点:
如果某个节点的深度比该树的其他节点都大,那么我们称之为最深的节点。
2 子树:
一个节点的子树是指该节点的所有后代节点和该节点本身。
返回

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.
我们返回val值为2 的节点,也就是图中黄色的节点。
图中标蓝色的节点就是该树的最深节点。
输入[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]是该树的节点序列。
输出是 [2, 7, 4] 这个子树的节点序列。
每一个输入和输出都有TreeNode节点类型。

Note:
The number of nodes in the tree will be between 1 and 500.
The values of each node are unique.
节点数量在1到500之间,包括1和500
每个节点的 val 值都是唯一的,不重复的。

2. 解题思路

本题的解决思路参考了https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/solution/ 的方法2,有兴趣的读者可以直接去看原文。

我们可以换一个角度看待问题,从深度转换为高度。
假设每个叶子节点的高度都是1 ,叶子节点上面那层节点的高度是2,逐层向上递增。
对于某一个节点来说,它的高度是在左子树的高度和右子树的高度中,选取最大的值加一。
于是这个问题就转化为了,为了让整个树的根节点的高度值达到最大,该从哪个分支向上走最好。可以使用深度优先搜索+递归的方式计算。
我们可以设计这样一个类,nodeAndHeight, 包括了一个节点和一个高度值,用它作为递归函数的返回值。

具体实现过程可以参考下面代码:

实现代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class nodeAndHeight{
    TreeNode node;
    int height;
    public nodeAndHeight(TreeNode node, int height){
        this.node = node;
        this.height = height;
    }
}
class Solution {
    public TreeNode subtreeWithAllDeepest(TreeNode root) {
        return dfs(root).node;
    }
    public nodeAndHeight dfs(TreeNode node){
        if(node == null){
            return (new nodeAndHeight(null, 0));
        }
        nodeAndHeight l = dfs(node.left);//左子树返回值
        nodeAndHeight r = dfs(node.right);//右子树返回值
        //如果左子树的高度大于右子树的高度
        //node节点的高度就是左子树的高度+1
        //能够帮助node节点达到最大高度的分支节点是左子树返回值中的节点l.node
        if(l.height > r.height){
            return (new nodeAndHeight(l.node, l.height+1));
        }
        if(l.height < r.height){
            return (new nodeAndHeight(r.node , r.height+1));
        }
        //如果两个子树的高度一样,返回自身节点以及子树高度+1
        return (new nodeAndHeight(node , r.height+1));
    }
}

3. 参考资料

https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/solution/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值