leetcode笔试题二叉树的前序、中序、后序遍历的递归和循环c++实现

#include <iostream>
#include <vector>
#include <stack>
using namespace std;
struct binaryTreeNode
{
    int                    m_nValue;
    binaryTreeNode*        m_pLeft;
    binaryTreeNode*        m_pRight;
};
void PrintTreeNode(binaryTreeNode* pNode)
{
    if (pNode != nullptr)
    {
        cout << "value of this node is" << pNode->m_nValue << endl;

        if (pNode->m_pLeft != NULL)
            cout << "value of its left child is" << pNode->m_pLeft->m_nValue << endl;
        else
            cout << "left child is null." << endl;

        if (pNode->m_pRight != NULL)
            printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);
        else
            printf("right child is null.\n");
    }
    else
    {
        printf("this node is null.\n");
    }

    printf("\n");
}
void PrintTree(binaryTreeNode* pRoot)
{
    PrintTreeNode(pRoot);//连续的调用这个函数去显示二叉树
    if (pRoot != NULL)
    {
        if (pRoot->m_pLeft != NULL)
            PrintTree(pRoot->m_pLeft);

        if (pRoot->m_pRight != NULL)
            PrintTree(pRoot->m_pRight);
    }
}
binaryTreeNode* CreateBinaryTreeNode(int value)
{
    binaryTreeNode *pNode = new binaryTreeNode();
    pNode->m_nValue = value;
    pNode->m_pLeft = nullptr;
    pNode->m_pRight = nullptr;
    return pNode;
}
// 将父节点与子节点连接起来
void ConnectTreeNodes(binaryTreeNode* pParent, binaryTreeNode* pLeft, binaryTreeNode* pRight)
{
    if (pParent != nullptr)
    {
        pParent->m_pLeft = pLeft;
        pParent->m_pRight = pRight;
    }

}
vector<int> result;
//中序递归
vector<int> InoderTraversal(binaryTreeNode* root)
{
    if (root != nullptr)
    {
        if (root->m_pLeft != nullptr)
            InoderTraversal(root->m_pLeft);
        result.push_back(root->m_nValue);
        if (root->m_pRight != nullptr)
            InoderTraversal(root->m_pRight);
    }
    return result;
}
//循环
vector<int> InoderTraversal1(binaryTreeNode* root)
{
    stack<binaryTreeNode*>stack;
    vector<int>v;
    while (stack.size() > 0||root!=nullptr)
    {
        if (root != nullptr)
        {
            stack.push(root);
            root = root->m_pLeft;
        }
        else
        {
            if (stack.size()>0)
            {
                root = stack.top();//获得尾指针
                stack.pop();
                v.push_back(root->m_nValue);
                root = root->m_pRight;
            }
        }   
    }
    return v;
}
//前序递归
vector<int> PreoderTraversal(binaryTreeNode* root)
{
    if (root != nullptr)
    {
        result.push_back(root->m_nValue);
        if (root->m_pLeft != nullptr)
            PreoderTraversal(root->m_pLeft);
        if (root->m_pRight != nullptr)
            PreoderTraversal(root->m_pRight);
    }
    return result;
}

//循环,同样可以用栈实现
vector<int> PreoderTraversal1(binaryTreeNode *root)
{
    vector<int> v;
    vector<binaryTreeNode*> stack;
    if (root) {
        stack.push_back(root);
    }
    while (stack.size()>0) {
        binaryTreeNode* n = stack.back();
        v.push_back(n->m_nValue);
        stack.pop_back();
        if (n->m_pRight) {                //注意睡醒
            stack.push_back(n->m_pRight);
        }
        if (n->m_pLeft) {
            stack.push_back(n->m_pLeft);
        }
    }
    return v;
}
//后序遍历递归
vector<int> PostoderTraversal(binaryTreeNode* root)
{
    if (root != nullptr)
    {

        if (root->m_pLeft != nullptr)
            PostoderTraversal(root->m_pLeft);
        if (root->m_pRight != nullptr)
            PostoderTraversal(root->m_pRight);
        result.push_back(root->m_nValue);
    }
    return result;
}
// We have two methods here.
//   1) the first one acutally is pre-order but reversed the access order.
//   2) the second one is the traditional one 
vector<int> PostoderTraversal1(binaryTreeNode *root) {
    vector<int> v;
    vector<binaryTreeNode*> stack;
    if (root) {
        stack.push_back(root);
    }
    while (stack.size()>0) {
        binaryTreeNode *n = stack.back();
        stack.pop_back();
        v.push_back(n->m_nValue);
        if (n->m_pLeft) {
            stack.push_back(n->m_pLeft);
        }
        if (n->m_pRight) {
            stack.push_back(n->m_pRight);
        }
    }
    std::reverse(v.begin(), v.end());  // the trick
    return v;
}

// traditional and standard way.
// using the stack to simulate the recursive function stack.
vector<int>  PostoderTraversal2(binaryTreeNode *root) {
    vector<int> v;
    vector<binaryTreeNode*> stack;
    binaryTreeNode *node = root;
    binaryTreeNode *lastVisitNode = NULL;
    while (stack.size()>0 || node != NULL) {

        if (node != NULL) {
            // keep going the left
            stack.push_back(node);
            node = node->m_pLeft;
        }
        else {
            binaryTreeNode *n = stack.back();
            // left way is finsised, keep going to the right way
            if (n->m_pRight != NULL && lastVisitNode != n->m_pRight) {
                node = n->m_pRight;
            }
            else {
                // both left and right has been accessed.
                stack.pop_back();
                v.push_back(n->m_nValue);
                lastVisitNode = n;
            }
        }
    }
    return v;
}
//测试
int main()
{
    binaryTreeNode *p1 = CreateBinaryTreeNode(1);
    binaryTreeNode *p2 = CreateBinaryTreeNode(2);
    binaryTreeNode *p3 = CreateBinaryTreeNode(3);
    binaryTreeNode *p4 = CreateBinaryTreeNode(4);
    binaryTreeNode *p5 = CreateBinaryTreeNode(5);
    binaryTreeNode *p6 = CreateBinaryTreeNode(6);
    binaryTreeNode *p7 = CreateBinaryTreeNode(7);
    ConnectTreeNodes(p1, p2, p3);
    ConnectTreeNodes(p2, p6, p7);
    ConnectTreeNodes(p3, p4, nullptr);
    ConnectTreeNodes(p4, nullptr, p5);
    PrintTree(p1);
    vector<int>result = PostoderTraversal2(p1);
    for (auto c : result)
        cout << c << endl;
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值