文章

1

粉丝

0

获赞

0

访问

310

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

typedef struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

TreeNode* getLeftMax(TreeNode* root) {
    if (!root) return NULL;
    while (root->right) root = root->right;
    return root;
}
TreeNode* getRightMax(TreeNode* root) {
    if (!root) return NULL;
    while (root->right) root = root->right;
    return root;
}

int countNodes(TreeNode* root)
{
    if(!root)
    {
        return 0;
    }
    int count = 0;
    Link_queue  Q;
    Init_queue(Q);
    enter_queue(Q,root);
    TreeNode *p;
    while(p)
    {
        delete_queue(Q,p);

        if(p->left!=NULL && p->right!=NULL)
        {
            TreeNode *left = getLeftMax(p->left);
            TreeNode *right = getRightMax(p->right);
            int left_val = abs(left->val-p->val);
            int right_val = abs(right->val-p->val);
            if(left_val == right_val)
            {
        ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发