二叉树

二叉树的镜像

利用栈实现

  1. void MirrorNonRecurively(BinaryTreeNode *pNode)  
  2. {  
  3.     if(NULL == pNode)  
  4.         return;  
  5.     stack<BinaryTreeNode *> stackTreeNode;  
  6.     stackTreeNode.push(pNode);   
  7.     while(stackTreeNode.size())  //栈不为空时
  8.     {  
  9.         BinaryTreeNode *pNode = stackTreeNode.top();  
  10.         stackTreeNode.pop();  
  11.         if(NULL != pNode->Left || NULL != pNode->Right)  //交换左右子树位置
  12.        {  
  13.             BinaryTreeNode *pTemp = pNode->Left;  
  14.             pNode->Left = pNode->Right;  
  15.             pNode->Right = pTemp;  
  16.         }         
  17.         if(NULL != pNode->Left)  
  18.             stackTreeNode.push(pNode->Left);  
  19.         if(NULL != pNode->Right)  
  20.             stackTreeNode.push(pNode->Right);  
  21.     }  
  22. }  

利用递归

 void Mirror(TreeNode *pRoot) {
    if(NULL!=pRoot)
    {
        TreeNode *temp=pRoot->left;
        pRoot->left=pRoot->right;
        pRoot->right=temp;
        Mirror(pRoot->left);
       Mirror(pRoot->right);
    }
    }

二叉树第k层的节点个数

k=1,nums=1;

k>1时,节点个数为左子树的k-1层节点个数加上右子树的k-1层节点个数,利用递归

int  numlevel(Binary *node,int k)

{

if(node==NULL||k==0)

return 0;

if(K==1)

return 1;

return numlevel(node->left,k-1)+numlevel(node->right,k-1)
}

利用栈实现二叉树的前序遍历:根->左子树->右子树

void preOder(binary *root)

{

if(root==NULL)

return ;

stack<binary *>stacknode;

while(root!=NULL||stacknode.empty()==false)

{

while(root)

{

stacknode.push(root);

cout<<root->data<<endl;

root=root->left;

}

if(root==NULL)

{

root=stacknode.top();

root=root->right;

}

}

}

利用栈实现二叉树的中序遍历:左子树->根->右子树

void MidOrder(binary *root)

{

if(root==NULL)

return ;

stack<binary *>stacknode;

while(root!=NULL||stacknode.empty()==false)

{

while(root!=NULL)

{

stacknode.push(root);

root=root->left;

}

if(root==NULL)

{

root=stacknode.top();

cout<<root->data<<endl;

stacknode.pop();

root=root->right;

}

}

}

后序遍历:左子树->右子树->根

利用两个栈:s1栈:根,左子树,右子树

s2:根,右子树,左子树

void LastOrdee(binary *root)

{

if(root==NULL)

return;

stack<binary *>s1;

stack<binary *>s2;

binary *node=root;

s1.push(node);//根压入s1

while(s1.empty()==false)

{

node=s1.top();

s1.pop();

s2.push(node);//第一次是根压入s2,后右,左

if(node->left)

s1.push(node->left);//左压入s1

if(node->right)

s1.push(node->right);//右压入s1


}

while(s2.empty()==false)

{

node=s2.top();

s2.pop();

cout<<bode->data<<endl;

}

}

层次遍历,一层一层输出,先入先出,按照队列实现

void LevelOrderTravel(BinaryTreeNode* root)

 {    if (root == NULL)      

  return;   

 queue<BinaryTreeNode*> q;   

 q.push(root);    

while (q.empty() == false) 

{     

     root = q.front();  

      visit(root);    

    q.pop();      

  if (root->m_pLeft)       

     q.push(root->m_pLeft);   

     if (root->m_pRight)     

       q.push(root->m_pRight);   

 }
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值