文章

6

粉丝

0

获赞

0

访问

125

头像
2025 年 9 月第 1 次 408 月考试卷 - 第41题回答
数据结构
发布于2025年9月30日 18:15
阅读数 24

1.先序遍历递归地看每个节点是否符合

2.

count = 0; //记录符合节点要求的数量
int findbigest(TreeNode root){
    if(root == null)
        return null;
    whiile(root->right!=null){
        root=root->right;
    }
    return root->val;
}
int findsmallest(TreeNode root){
    if(root == null)
        return null;
    whiile(root->left!=null){
        root=root->left;
    }
    return root->left;
}
void func(TreeNode root){
    if(root == null)
        return;
    int a = findbigest(root->left);
    int b = findsmallest(root->right);
    if(abs(a - root->val)==abs(b - root->val) && root->left!=null && root->right!=null)
        count++;
    func(root->left);
    func(root->right);
}
int main(){
    func(root)
    return count;
}

3.时间复杂O(Nlog2N)空间O(log2N)

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发