zoukankan      html  css  js  c++  java
  • 二叉树的基础代码:遍历+深度

    求二叉树的深度(后序遍历)

    int Depth (BiTree T )       // 返回二叉树的深度

      if ( !T )    depthval = 0;

      else{

        depthLeft = Depth( T->lchild );

        depthRight= Depth( T->rchild );

        depthval = 1 + (depthLeft > depthRight ?depthLeft : depthRight);

        }    

      return depthval;

    }

    遍历二叉树

    void Preorder (BiTree T,void( *visit)(TElemType& e))

    {     // 先序遍历二叉树

    if (T) {

      visit(T->data);            // 访问结点

      Preorder(T->lchild, visit); // 遍历左子树

      Preorder(T->rchild, visit);// 遍历右子树

       }

    }

    void Inorder (BiTree T,void( *visit)(TElemType& e))

    {   // 中序遍历二叉树

    if (T) {

       Inreorder(T->lchild, visit); // 遍历左子树

      visit(T->data);       // 访问结点

      Inreorder(T->rchild, visit); // 遍历右子树

          }

    }

    void Postorder (BiTree T,void( *visit)(TElemType& e))

    {    // 后序遍历二叉树

    if (T) {

      Postreorder(T->lchild, visit);  // 遍历左子树

      Postreorder(T->rchild, visit);// 遍历右子树

      visit(T->data);         // 访问结点

         }

    }

  • 相关阅读:
    1. 马尔科夫决策过程
    梯度下降法、随机梯度下降法、小批量梯度下降法
    计算曲线与坐标轴的面积
    鼠标进入,显示div
    codewar
    Django firstDay
    C#学习之关于lock
    winfrom 界面中表格数据修改及刷新(DEV)
    SVN 分支合并 版本控制
    wpf 绑定非元素对象
  • 原文地址:https://www.cnblogs.com/hellochennan/p/6702349.html
Copyright © 2011-2022 走看看