두 갈래 나무 삭제 노드

2837 단어 두 갈래 나무
#include <iostream>
#include <string>
using namespace std;

struct TreeNode{
    int data;
    TreeNode *lchild;
    TreeNode *rchild;
};

TreeNode *findMin(TreeNode *&root){
    while (root->lchild)
        root = root->lchild;
    return root;
}

void DeleteNode(TreeNode *root, int x){
    if (!root)
        return ;

    if (x < root->data)
        DeleteNode(root->lchild, x);
    else if (x > root->data)
        DeleteNode(root->rchild, x);
    else if (root->lchild && root->rchild){
        TreeNode *min = findMin(root->rchild);
        root->data = min->data;
        DeleteNode(root->rchild, min->data);        
    }
    else{
        TreeNode *p = root;
        root = root->lchild ? root->lchild : root->rchild;
        delete p;
    }
}

int main()
{
    
    return 0;
}

 
 
 
 
 
EOF

좋은 웹페이지 즐겨찾기