首页 > 其他分享 >450. 删除二叉搜索树中的节点c

450. 删除二叉搜索树中的节点c

时间:2024-03-14 19:44:23浏览次数:18  
标签:right TreeNode struct 450 树中 二叉 return root left

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

struct TreeNode* leftleave(struct TreeNode* root){
    if(root->left){
        root=root->left;
        while(root&&root->right) root=root->right;
        return root;
    }
    return NULL;
}

struct TreeNode* deleteNode(struct TreeNode* root, int key){
    if(!root) return NULL;
    if(!root->left&&!root->right&&root->val==key){
        free(root);
        return NULL;
    }
    if(root->val>key){
        root->left=deleteNode(root->left,key);
    }else if(root->val<key){
        root->right=deleteNode(root->right,key);
    }else{
        struct TreeNode* temp=leftleave(root);
        if(temp){
            root->val=temp->val;
            root->left=deleteNode(root->left,temp->val);
        }else{
            temp=root->right;
            free(root);
            return temp;
        }
    }
    return root;
}

结果:

标签:right,TreeNode,struct,450,树中,二叉,return,root,left
From: https://www.cnblogs.com/llllmz/p/18073760

相关文章

  • 236. 二叉树的最近公共祖先c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*/structTreeNode*lowestCommonAncestor(structTreeNode*root,structTreeNode*p,structTreeNode*q){......
  • leedcode-完全二叉树的节点个数
    自己写的,使用广度优先BFS,迭代:classSolution:defcountNodes(self,root:Optional[TreeNode])->int:#如果根节点为空,则树中节点数为0ifnotroot:return0#初始化队列,并将根节点放入队列中queue=[root]......
  • 洛谷题单指南-二叉树-P5076 【深基16.例7】普通二叉树(简化版)
    原题链接:https://www.luogu.com.cn/problem/P5076题意解读:此题本质上是要实现一个二叉搜索树的功能。解题思路:从数据规模10^4来看,只要复杂度在n^2范围内基本上是可以通过的,下面给出两种做法:1、有序数组法对应5个操作的实现逻辑如下:操作一:查x的排名。直接通过二分查找>=x的第......
  • 501. 二叉搜索树中的众数c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*//***Note:Thereturnedarraymustbemalloced,assumecallercallsfree().*/intmax,sum,pre;void......
  • 98. 验证二叉搜索树c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*/boolinorder(structTreeNode*root,long*pre){if(!root)returntrue;boola=inorder(root->le......
  • 617. 合并二叉树c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*/structTreeNode*mergeTrees(structTreeNode*root1,structTreeNode*root2){if(!root1&&!roo......
  • 257. 二叉树的所有路径c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*//***Note:Thereturnedarraymustbemalloced,assumecallercallsfree().*/chartemp[200]={0};v......
  • 洛谷题单指南-二叉树-P4913 【深基16.例3】二叉树深度
    原题链接:https://www.luogu.com.cn/problem/P4913题意解读:计算二叉树的深度解题思路:首先介绍二叉树的存储方式,对于本题,直接用数组模拟,数组的下标即节点号structnode{intl,r;}tree[N];tree[i].l存的是节点i的左子结点,tree[i].r存的是节点i的右子节点。计算深度至......
  • 【BFS二叉树】113路径总和II
    113路径总和II给你二叉树的根节点root和一个整数目标和targetSum,找出所有从根节点到叶子节点路径总和等于给定目标和的路径。思路:题目最终输出的是路径,因此用BFS遍历的时候,需要记录走到每个节点的路径;又因为路径和是要等于某个目标值的,因此也需要记录目标和。⇒......
  • 数据结构之树(Topk问题, 链式二叉树)
    一.topk问题取N个数中最大(小)的前k个值,N远大于k这道题可以用堆的方法来解决,首先取这N个数的前k个值,用它们建堆时间复杂度O(k)之后将剩余的N-k个数据依次与堆顶数据进行比较,如果比堆顶数据大,则将堆顶数据覆盖后向下调整时间复杂度(N-k)*log(N)总共的时间复杂度为O(......