#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 ;
} int depthLeft = getTreeDepth(root->m_pLeft);
int depthRight = getTreeDepth(root->m_pRight);
return (depthLeft > depthRight) ? depthLeft + : depthRight + ; } void PrePrintTree(BinaryTreeNode* root){
if(root != NULL){
printf("%d ",root->m_value);
}else{
return;
} PrePrintTree(root->m_pLeft);
PrePrintTree(root->m_pRight);
printf("\n");
} 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 = ;
node2->m_value = ;
node3->m_value = ;
node4->m_value = ;
node5->m_value = ;
node6->m_value = ;
node7->m_value = ;
node8->m_value = ;
node9->m_value = ;
node10->m_value = ;
node11->m_value = ; printf("%d\n\n",getTreeDepth(root)); PrePrintTree(root);
}

剑指offer 39_二叉树的深度的更多相关文章

  1. 【Java】 剑指offer(55-1) 二叉树的深度

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过 ...

  2. Go语言实现:【剑指offer】二叉树的深度

    该题目来源于牛客网<剑指offer>专题. 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. Go语言实现: /** ...

  3. 《剑指offer》二叉树的深度

    本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:

  4. 剑指Offer 38. 二叉树的深度 (二叉树)

    题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 题目地址 https://www.nowcoder.com/prac ...

  5. 剑指offer:二叉树的深度

    题目描述: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 解题思路: 这道题也是递归的思路,比较简单. 做的过程中遇到的一个 ...

  6. [剑指Offer] 38.二叉树的深度

    题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. [思路1]递归 /* struct TreeNode { int v ...

  7. 剑指offer——60二叉树的深度

    题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.   题解: 简单的深度遍历即可.   class Solution ...

  8. 【剑指Offer】二叉树的深度 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://www.nowcoder.co ...

  9. 剑指 Offer 34. 二叉树中和为某一值的路径

    剑指 Offer 34. 二叉树中和为某一值的路径 输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径.从树的根节点开始往下一直到叶节点所经过的节点形成一条路径. 示例: 给定如下 ...

随机推荐

  1. APP Store审核太慢?大神分享快速审核的小技巧

    这节课要来跟大家讲讲怎样可以快速的有效的通过苹果的审核,大家可能也有听说过苹果的快审,苹果的审核流程除了机器审核还有一个人工的审核,以前的苹果审核时用到的时间也是不短,一些有经验的开发者可能也会发现在 ...

  2. Codeforces 869C The Intriguing Obsession:组合数 or dp

    题目链接:http://codeforces.com/problemset/problem/869/C 题意: 红色.蓝色.紫色的小岛分别有a,b,c个. 你可以在两个不同的岛之间架桥,桥的长度为1. ...

  3. 智课雅思词汇---二十、前缀syn-sym-syl是什么意思

    智课雅思词汇---二十.前缀syn-sym-syl是什么意思 一.总结 一句话总结:l,m,n是可以互换 前缀:sy-, syn-, sym-, syl- [词根含义]:共同,同时 [词根来源]:(s ...

  4. 企业环境中部署 ActiveMQ

    这一章讲述了怎么配置 ActiveMQ 集群.

  5. asp.net JS取值

    <script> $(function () { $.post("RegisterNew.aspx", { "ddlprovince": $(&qu ...

  6. The tag handler class for "c:set"(org.apache.taglibs.standard.tag.rt.core.UrlTag)was not found on the Java Build Path

    1.源码: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> < ...

  7. Memcache笔记(1)

    缓存主要分为:页面缓存和数据缓存 Memcache .redis.mongodb都是做数据缓存的 Memcache是什么? 是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的ha ...

  8. poj2942 点-双联通+二分图染色

    题意:有一群骑士要坐在一个圆形的桌子上,他们之间有些人相互讨厌,所以不能挨着,要求算出一次也不能坐在桌子上的人,每次会议桌子必须奇数个人,一个人不能开会 题解:可以先建一个补图,要满足题目条件我们只要 ...

  9. 一个android好博客

    http://blog.csdn.net/eastmount http://lishuaishuai.iteye.com/ 二维码:http://www.cnblogs.com/liuan/categ ...

  10. 从AD域获取用户AD信息

    public static Dictionary<string, string> SearchADInfo(string adName) { string strTemp = " ...