865. Smallest Subtree with all the Deepest Nodes

865. Smallest Subtree with all the Deepest Nodes

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:

  1. We return the node with value 2, colored in yellow in the diagram.
  2. The nodes colored in blue are the deepest nodes of the tree.
  3. The input “[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]” is a serialization of the given tree.
  4. The output “[2, 7, 4]” is a serialization of the subtree rooted at the node with value 2.
  5. Both the input and output have TreeNode type.

Note:

  1. The number of nodes in the tree will be between 1 and 500.
  2. The values of each node are unique.

方法1: recursion

思路:

从递归的角度来思考,对于root节点,需要从左右紫薯的递归函数知道什么才可以来向上推一步答案?关键需要比较的是左右紫薯的深度。如果左子树比较深,那么这个最深的紫薯和root 以及root -> right 的紫薯都没有关系,当然也不一定是root->left,也可能是很底层很遥远的一个节点。为了递归最后能传回这个关键节点,我们考虑将其作为返回值的一部分。而另一部分,left/right还应该通知root他们的深度各自是多少。这样就决定了我们的返回值需要是一对pair<\int, TreeNode*>,{depth, resNode}。那么比如root->left.depth比较大,我们将左边传回来的resNode继续向上传,和{depth + 1, resNode}。注意这里depth如实的叠加1获得root实际的深度。但是当root -> left.depth == root -> right.depth,左右紫薯都包括deepest node,返回的应该是{depth + 1, root}。

Complexity

Time complexity: O(n)
Space complexity: O(h)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* subtreeWithAllDeepest(TreeNode* root) {
        return subHelper(root, 0).second;
    }
    
    pair<int, TreeNode*> subHelper(TreeNode* root, int height){
        if (!root) return {height, root};
        auto lh = subHelper(root -> left,  height + 1);
        auto rh = subHelper(root -> right, height + 1);
        
        if (lh.first != rh.first) return (lh.first > rh.first) ? make_pair(lh.first + 1, lh.second) : make_pair(rh.first + 1, rh.second);
        return {lh.first + 1, root};
    }
};

小修改:用bottom up计算height的写法,不需要全球变量。

class Solution {
public:
    TreeNode* subtreeWithAllDeepest(TreeNode* root) {
        return subHelper(root).second;
    }
    
    pair<int, TreeNode*> subHelper(TreeNode* root){
        if (!root) return {-1, root};
        auto lh = subHelper(root -> left);
        auto rh = subHelper(root -> right);
        
        if (lh.first != rh.first) return (lh.first > rh.first) ? make_pair(lh.first + 1, lh.second) : make_pair(rh.first + 1, rh.second);
        return {lh.first + 1, root};
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值