zoukankan      html  css  js  c++  java
  • 求一个二叉树的深度

    求一个二叉树的深度,是这样理解这个问题的。

    如果这个棵树为空,那么他的深度为0

    如果一个树只有一个节点,那么他的深度为1

    如果根节点只有左子,没有右子,那么他的深度为左子树的深度+1

    如果根节点只有右子,没有左子,那么他的深度为右子树的深度+1

    如果根节点既有左子,又有右子,那么他的深度为左子右子较大的那个深度+1

    struct BNode

    {

          int data;   //数据域

          BNode* left;//左子

          BNode* right;//右子
    }

    int TreeDepth(BNode* node)

           if(node==null)

              return 0;

           int nLeft=TreeDepth(node->left);

           int nRight=TreeDepth(node->right);

           return (nLeft>nRight)?nLeft+1:nRight+1;

    }

  • 相关阅读:
    循环逗号分割数组!
    只是想好好学习一下!
    html元素水平垂直居中
    闭包知识点--笔记
    20160314
    从零开始做运维-零
    Nginx 和 CodeIgniter
    安装PIL库
    init
    NutUI3 多端实践之路
  • 原文地址:https://www.cnblogs.com/zhousilai/p/4440479.html
Copyright © 2011-2022 走看看