zoukankan      html  css  js  c++  java
  • 【剑指offer】二叉树的深度

    题目链接:二叉树的深度


    题意:输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

     

    题解:这个题真是数据结构里写烂的一道题。。递归左右子树深度即可。

     

    代码:

     1 /*
     2 struct TreeNode {
     3     int val;
     4     struct TreeNode *left;
     5     struct TreeNode *right;
     6     TreeNode(int x) :
     7             val(x), left(NULL), right(NULL) {
     8     }
     9 };*/
    10 class Solution {
    11 public:
    12     int TreeDepth(TreeNode* pRoot)
    13     {
    14         if(pRoot == NULL)     return 0;
    15         int left = TreeDepth(pRoot->left);
    16         int right =TreeDepth(pRoot->right);
    17         if(left > right)    return left+1;
    18         else    return right+1;
    19     }
    20 };
  • 相关阅读:
    什么是https?
    Gojs
    GoJs 01讲解
    你真的了解WebSocket吗?
    django channels
    序列化及反序列化
    全角转半角
    Thread Culture
    设置输入法
    token的认证使用
  • 原文地址:https://www.cnblogs.com/Asumi/p/12416662.html
Copyright © 2011-2022 走看看