• 剑指offer 39_二叉树的深度


    #include <stdio.h>
    #include <malloc.h>
    
    typedef int Item;
    typedef struct node{
        Item m_value;
        struct node* m_pLeft;
        struct node* m_pRight;
    }BinaryTreeNode;
    
    
    int getTreeDepth(BinaryTreeNode *root){
    
        if(root == NULL){
            return 0;
        }
    
        int depthLeft = getTreeDepth(root->m_pLeft);
        int depthRight = getTreeDepth(root->m_pRight);
        return (depthLeft > depthRight) ? depthLeft + 1 : depthRight + 1;
        
    }
    
    
    void PrePrintTree(BinaryTreeNode* root){
        if(root != NULL){
            printf("%d  ",root->m_value);
        }else{
            return;
        }
        
        PrePrintTree(root->m_pLeft);
        PrePrintTree(root->m_pRight);
        printf("
    ");
    }
    
    int main(){
        BinaryTreeNode* root = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        BinaryTreeNode* node2 = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        BinaryTreeNode* node3 = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        root->m_pLeft = node2;
        root->m_pRight = node3;
    
    
        BinaryTreeNode *node4 = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        BinaryTreeNode *node5 = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        BinaryTreeNode *node6 = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        node2->m_pLeft = node4;
        node2->m_pRight = node5;
        node3->m_pRight = node6;
    
        BinaryTreeNode *node7 = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        node5->m_pLeft = node7;
    
        BinaryTreeNode *node8 = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        BinaryTreeNode *node9 = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        node7->m_pLeft = node8;
        node7->m_pRight = node9;
    
        BinaryTreeNode *node10 = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        BinaryTreeNode *node11 = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
        node9->m_pLeft = node10;
        node10->m_pLeft = node11;
    
        root->m_value = 1;
        node2->m_value = 2;
        node3->m_value = 3;
        node4->m_value = 4;
        node5->m_value = 5;
        node6->m_value = 6;
        node7->m_value = 7;
        node8->m_value = 8;
        node9->m_value = 9;
        node10->m_value = 10;
        node11->m_value = 11;
    
        
        printf("%d
    
    ",getTreeDepth(root));
        
        PrePrintTree(root);
    }
  • 相关阅读:
    解惑丨C语言字符串常量、字符数组、字符指针!
    程序员压根就不想找对象?谁说的,给我站出来!
    C/C++实习工作应该具备那些能力?才能拿更好的工作和薪资!
    mysql查看创建数据表的DDL语句
    .NET 5 ML.NET 部署运行时出现 Unable to load DLL MklImports 的处理方法
    canvas波浪扇形
    小程序 Canvas 倒计时组件 (React 版)
    Canvas 倒计时
    策略模式实战中多种写法
    MySQL-基础架构介绍
  • 原文地址:https://www.cnblogs.com/maxiaodoubao/p/4738927.html
Copyright © 2020-2023  润新知