二叉树面试题—前,中,后序(非递归),判断是否为完全二叉树

二叉树的基本操作

Node* GetParent(Node* x) //得到父节点
    {
        if ( _pRoot == NULL)
            return _pRoot;
        return _GetParent(_pRoot, x);
    }

    Node* Find(const T& value) //查找结点
    {
        return _FindNode(_pRoot, value);
    }

    size_t Hight()  //求树的高度
    {
        return _Hight(_pRoot);
    }

    size_t GetLeefNode() //求叶子节点的个数
    {
        return _GetLeefNode(_pRoot);
    }

    size_t GetLevelNode(size_t k)   //求第K层节点的个数
    {
        return _GetLevelNode(_pRoot,k);
    }

    void PreOrder_Nor()   //前序遍历(不用递归)
    {
        if (_pRoot)
            _PreOrder_Nor(_pRoot);
    }

    void InOrder_Nor()   //中序遍历(不用递归)
    {
        if (_pRoot)
            _InOrder_Nor(_pRoot);
    }

    void PostOrder_Nor()   //后序遍历(不用递归)
    {
        if (_pRoot)
            _PostOrder_Nor(_pRoot);
    }

    void GetBinaryMirror()   //求二叉树的镜像
    {
        if (_pRoot)
             _GetBinaryMirror(_pRoot);
    }

    void GetBinaryMirror_Nor()   //求二叉树的镜像(非递归)
    {
        if (_pRoot)
            _GetBinaryMirror(_pRoot);
    }

    bool isCompleteBinaryTree()   //判断是否为完全二叉树
    {
        if (_pRoot)
        {
            queue<Node*> q;
            q.push(_pRoot);
            bool isComplete = true;
            while (!q.empty())
            {
                Node* Front = q.front();
                q.pop();
                if (!isComplete && (Front->_pLeft || Front->_pRight))
                {
                    return false;
                }
                if (Front->_pLeft)
                    q.push(Front->_pLeft);
                if (Front->_pRight)
                    q.push(Front->_pRight);
                else 
                {
                    isComplete = false;
                }
            }
            return true;
        }
        return false;
    }
    Node* _GetParent(Node* proot, Node* x)
    {
        if (proot->_pLeft == x || proot ->_pRight == x)
            return proot; 
        if (proot->_pLeft == NULL || proot->_pRight == NULL)
            return NULL;
        Node* Temp = _GetParent(proot->_pLeft, x);
        if (Temp == NULL)
        {
            Temp = _GetParent(proot->_pRight, x);
        }
         return Temp;
    }

    Node* _FindNode(Node* proot, const T& value)
    {
        if ( proot == NULL || proot->_value == value)
            return proot;
        Node* Temp = _FindNode(proot->_pLeft,value);
        if (Temp == NULL)
        {
            Temp = _FindNode(proot->_pRight, value);
        }
        return Temp;
    }

    size_t _Hight(Node* proot)
    {
        if (proot == NULL)
            return 0;
        size_t LeftHight = 1+_Hight(proot->_pLeft);
        size_t RightHight = 1+_Hight(proot->_pRight);
        return (LeftHight>RightHight)?LeftHight:RightHight;
    }

    size_t _GetLeefNode(Node* proot)
    {
        if (proot == NULL)
            return 0;
        if (proot->_pLeft == NULL && proot ->_pRight == NULL)
            return 1;
        size_t LeftLeefNode = _GetLeefNode(proot->_pLeft);
        size_t RightLeefNode = _GetLeefNode(proot->_pRight);
            return (LeftLeefNode+RightLeefNode);
    }

    size_t _GetLevelNode(Node* proot, size_t k)
    {
        size_t h = _Hight(proot);
        if (k == 1)
            return 1;
        if (  proot == NULL || k> h )
            return 0;

        size_t LeftLevelNode  = _GetLevelNode(proot->_pLeft,k-1);
        size_t RightLevelNode = _GetLevelNode(proot->_pRight,k-1);
        return (LeftLevelNode+RightLevelNode);

    }

    void _PreOrder_Nor(Node* proot)
    {
        cout<<"前序:";
        stack<Node*> st;
        st.push(proot);
        while (!st.empty())
        {
            Node* temp = st.top();
            cout<<temp->_value<<"->";
            st.pop();
            if ( temp->_pRight )
                st.push(temp->_pRight);
            if ( temp->_pLeft )
                st.push(temp->_pLeft);      
        }
        cout<<"end"<<endl;
    }

    void _InOrder_Nor(Node* proot)
    {
        cout<<"中序:";
        stack<Node*> st;
        Node* temp = proot;
        while ( temp || !st.empty() )
        {
            while (temp)
            {
                st.push(temp);
                temp = temp->_pLeft;
            }
            Node* Top = st.top();
            st.pop();
            cout<<Top->_value<<"->";
            temp = Top->_pRight;
        }
        cout<<"end"<<endl;
    }

    void _PostOrder_Nor(Node* proot)
    {
        cout<<"后序:";
        stack<Node*> st;
        Node* pCur = proot;
        Node* pPre = NULL;

        while ( pCur || !st.empty() )
        {
            while (pCur)
            {
                st.push(pCur);
                pCur = pCur->_pLeft;
            }

            pCur = st.top();
            if ( pCur->_pRight == NULL || pCur ->_pRight  == pPre)
            {
                cout<<pCur->_value<<"->";
                st.pop();
                pPre = pCur;
                pCur = NULL;
            }
            else 
                pCur = pCur->_pRight;
        }

        cout<<"end"<<endl;
    }

    void _GetBinaryMirror(Node* &proot)
    {
        if (proot == NULL)
            return;
        if (proot ->_pLeft  || proot->_pRight )
            swap(proot->_pLeft, proot->_pRight);
        _GetBinaryMirror(proot->_pLeft);
        _GetBinaryMirror(proot->_pRight);
    }

    void _GetBinaryMirror_Nor(Node* &proot)
    {
        queue<Node*> q;
        q.push(proot);
        while(!q.empty())
        {
            Node* pCur = q.front();
            q.pop();
            std::swap(pCur->_pLeft, pCur->_pRight);
            if (pCur->_pLetf)
                q.push(pCur->_pLetf);
            if (pCur->_pRight)
                q.push(pCur->_pRight);
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魏尔肖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值