• 二叉树的深度


    题目:输入一棵二叉树的根节点,求该树的深度。从根节点到叶子结点一次经过的结点形成树的一条路径,最长路径的长度为树的深度。根节点的深度为1。

    思路:如果根节点为空,则深度为0,返回0,递归的出口,如果根节点不为空,那么深度至少为1,然后我们求他们左右子树的深度,比较左右子树深度值,返回较大的那一个,通过递归调用

      1 #include<stdio.h>
      2 #include "stdafx.h"
      3 
      4 struct BinaryTreeNode
      5 {
      6     int              m_nValue;
      7     BinaryTreeNode*  m_pLeft;
      8     BinaryTreeNode*  m_pRight;
      9 };
     10 
     11 BinaryTreeNode* CreateBinaryTreeNode(int value)
     12 {
     13     BinaryTreeNode* pNode = new BinaryTreeNode();
     14     pNode->m_nValue = value;
     15     pNode->m_pLeft = NULL;
     16     pNode->m_pRight = NULL;
     17 }
     18 
     19 void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
     20 {
     21     if(pParent != NULL)
     22     {
     23         pParent->m_pLeft = pLeft;
     24         pParent->m_pRight = pRight;
     25     }
     26 }
     27 
     28 void PrintTreeNode(BinaryTreeNode* pNode)
     29 {
     30     if(pNode != NULL)
     31     {
     32         printf("value of this node is: %d
    ", pNode->m_nValue);
     33         
     34         if(pNode->m_pLeft != NULL)
     35             printf("value of its left child is: %d.
    ", pNode->m_pLeft->m_nValue);
     36         else
     37             printf("left child is null.
    ");
     38         
     39         if(pNode->m_pRight != NULL)
     40             printf("value of its right child is: %d.
    ",pNode->m_pRight->m_nValue);
     41         else
     42             printf("right child is null.
    ");
     43     }
     44     else
     45     {
     46         printf("this node is null.
    ");
     47     }
     48     printf("
    ");
     49 }
     50 
     51 void PrintTree(BinaryTreeNode* pRoot)
     52 {
     53     PrintTreeNode(pRoot);
     54     
     55     if(pRoot != NULL)
     56     {
     57         if(pRoot->m_pLeft != NULL)
     58             PrintTree(pRoot->m_pLeft);
     59         
     60         if(pRoot->m_pRight != NULL) 
     61             PrintTree(pRoot->m_pRight);
     62     }
     63 }
     64 
     65 void DestroyTree(BinaryTreeNode* pRoot)
     66 {
     67     if(pRoot != NULL)
     68     {
     69         BinaryTreeNode* pLeft = pRoot->m_pLeft;
     70         BinaryTreeNode* pRight = pRoot->m_pRight;
     71         
     72         delete pRoot;
     73         pRoot = NULL;
     74         
     75         DestroyTree(pLeft);
     76         DestroyTree(pRight);
     77     }
     78 }
     79 
     80 int TreeDepth(BinaryTreeNode* pRoot)
     81 {
     82     if(pRoot == NULL)
     83         return 0;
     84     
     85     int nLeft = TreeDepth(pRoot->m_pLeft);
     86     int nRight = TreeDepth(pRoot->m_pRight);
     87     
     88     return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
     89 }
     90 
     91 //            1
     92 //         /      
     93 //        2        3
     94 //       /         
     95 //      4  5         6
     96 //        /
     97 //       7
     98 
     99 int main()
    100 {
    101     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    102     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    103     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    104     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    105     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    106     BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    107     BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
    108     
    109     ConnectTreeNodes(pNode1, pNode2, pNode3);
    110     ConnectTreeNodes(pNode2, pNode4, pNode5);
    111     ConnectTreeNodes(pNode3, NULL,   pNode6);
    112     ConnectTreeNodes(pNode5, pNode7, NULL  );
    113     
    114     int result = TreeDepth(pNode1);
    115     printf("The depth of binarytree is %d
    ", result);
    116     
    117     DestroyTree(pNode1);
    118     return 0;
    119 }

  • 相关阅读:
    [转]Could not load file or assembly 'XXX' or one of its dependencies.
    网页上显示别人电脑没安装的字体,例如LED字体
    JS 保留小数点后面2位小数
    ASP.NET2.0揭秘读书笔记五——维护应用程序状态之cookie
    C#高级编程读书笔记之.NET体系结构
    ASP.NET2.0揭秘读书笔记之八——页面输出缓存
    《大话设计模式》读书笔记一 简单工厂模式
    C#高级编程读书笔记之继承
    ASP.NET 2.0揭秘读书笔记七——使用用户配置文件Profile
    终于成功安装了SQL SqlServer2005
  • 原文地址:https://www.cnblogs.com/sankexin/p/5635482.html
Copyright © 2020-2023  润新知