LeetCode 865. Smallest Subtree with all the Deepest Nodes (Medium)

Description:

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:
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.
Note:
  • The number of nodes in the tree will be between 1 and 500.
  • The values of each node are unique.

Analysis:

Actually, the problem requires us to find the first node whose left subtree and right subtree have the same height from the first level of the tree to the last level of the tree. Here, we stipulate the metric, the height of a leaf node is 1, and the height of a subtree of a leaf node is 0.

We can use a recursion to find the target node.

  • If the left subtree and the right subtree of the current node have the same height, we just return the current node.
  • If the height of the left subtree is lower than that of the right subtree, we continue to find the target node in the right subtree.
  • If the height of the left subtree is higher than that of the right subtree, we continue to find the target node in the left subtree.

Code:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode subtreeWithAllDeepest(TreeNode root) {
        if(root == null) {
            return null;
        }else{
            int leftHeight = getHeight(root.left);
            int rightHeight = getHeight(root.right);
            if(leftHeight == rightHeight) {
                return root;
            }else if(leftHeight < rightHeight) {
                return subtreeWithAllDeepest(root.right);
            }else {
                return subtreeWithAllDeepest(root.left);
            } 
        }
    }
    
    public int getHeight(TreeNode node) {
        if(node == null) {
            return 0;
        }else{
            int leftHeight = 0;
            int rightHeight = 0;
            if(node.left != null) {
                leftHeight = getHeight(node.left);
            }
            if(node.right != null) {
                rightHeight = getHeight(node.right);
            }
            
            int height = 1 + Math.max(leftHeight, rightHeight);
            
            return height;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值