剑指offer----面试题8 二叉树的下一个节点

具体题目看剑指OFFER书面试题8,本博文仅供博主代码笔记只用,如需转载请私信征得博主同意

本例主要需要学习:

1.输出层次分明的二叉树(先序遍历)

2.构建有父指针的二叉树的算法

3.销毁二叉树的算法

4.查找二叉树找那个任意指定的节点,按中序遍历的下一个节点

//#include <stdio.h>
//#include <stdlib.h>
#include <iostream>
using namespace std;

struct BinaryTreeNode
{
    int value;
    BinaryTreeNode* leftchild;
    BinaryTreeNode* rightchild;
    BinaryTreeNode* parent;
};

BinaryTreeNode* CreateTreeNode(int m_value)
{
    BinaryTreeNode* pNode = new BinaryTreeNode();
    pNode->value = m_value;
    pNode->leftchild = nullptr;
    pNode->rightchild = nullptr;
    pNode->parent = nullptr;

    return pNode;
}

void ConnectNode(BinaryTreeNode* Con_parent, BinaryTreeNode* Con_lchild,
    BinaryTreeNode* Con_rchild)
{
    Con_parent->leftchild = Con_lchild;
    Con_parent->rightchild = Con_rchild;
    Con_lchild->parent = Con_parent;
    Con_rchild->parent = Con_parent;
}

void DispBTNode(BinaryTreeNode* b)
{    
    //若要层次更分明可考虑层次遍历输出,层次遍历代码参考我的根据中序先序重建二叉树的博文

    //https://blog.csdn.net/qq_34793133/article/details/80582358


    cout << "二叉树输出如下,以括号标明层次:" << endl;
    if (b != nullptr)
    {
        cout << b->value;
    }

    if (b->leftchild != nullptr || b->rightchild != nullptr)
    {
        cout << '(';

        DispBTNode(b->leftchild);
        if (b->rightchild != nullptr)
            cout << ',' ;
        DispBTNode(b->rightchild);

        cout << ")";

    }

    cout << endl;
}


void DestroyBinaryTree(BinaryTreeNode* Root)
{
    if (Root != nullptr)
    {
        BinaryTreeNode* tmp_leftchild = Root->leftchild;
        BinaryTreeNode* tmp_rightchild = Root->rightchild;

        delete Root; Root = nullptr;

        DestroyBinaryTree(Root->leftchild);
        DestroyBinaryTree(Root->rightchild);
    }
        
}


BinaryTreeNode* GetNextNode(BinaryTreeNode* pNode)
{
    if (pNode == nullptr)
        return nullptr;

    if (pNode->rightchild != nullptr)
    {
        BinaryTreeNode* temp1 = pNode->rightchild;
        while (temp1->leftchild != nullptr)
        {
            temp1 = temp1->leftchild;
        }

        return temp1;
    }


    else if (pNode->parent != nullptr)
    {
        BinaryTreeNode* ParentNode = pNode->parent;
        BinaryTreeNode* CurrentNode = pNode;

        while (ParentNode != nullptr&&ParentNode->rightchild != CurrentNode)
        {
            CurrentNode = ParentNode; ParentNode = CurrentNode->parent;
        }

        return ParentNode;
    }
}


// ====================测试代码====================
void Test(char* testName, BinaryTreeNode* pNode, BinaryTreeNode* expected)
{
    if (testName != nullptr)
        printf("%s begins: ", testName);

    BinaryTreeNode* pNext = GetNextNode(pNode);
    if (pNext == expected)
        printf("Passed.\n");
    else
        printf("FAILED.\n");
}


//            8
//        6      10
//       5 7    9  11

int main(void)
{
    BinaryTreeNode* pNode8 = CreateTreeNode(8);
    BinaryTreeNode* pNode6 = CreateTreeNode(6);
    BinaryTreeNode* pNode10 = CreateTreeNode(10);
    BinaryTreeNode* pNode5 = CreateTreeNode(5);
    BinaryTreeNode* pNode7 = CreateTreeNode(7);
    BinaryTreeNode* pNode9 = CreateTreeNode(9);
    BinaryTreeNode* pNode11 = CreateTreeNode(11);

    ConnectNode(pNode8, pNode6, pNode10);
    ConnectNode(pNode6, pNode5, pNode7);
    ConnectNode(pNode10, pNode9, pNode11);

    DispBTNode(pNode8);

    Test("Test1", pNode8, pNode9);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_34793133/article/details/80619153