zoukankan      html  css  js  c++  java
  • LeetCode 剑指offer 面试题55

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

    例如:

    给定二叉树 [3,9,20,null,null,15,7],


    返回它的最大深度 3 。

    提示:

    节点总数 <= 10000

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if(!root) return 0;
            return dfs(root,1);
        }
        int dfs(TreeNode* root,int depth)
        {
            if(!root->left&&!root->right) return depth;
            else if(!root->left&&root->right) return dfs(root->right,depth+1);
            else if(!root->right&&root->left) return dfs(root->left,depth+1);
            else return max(dfs(root->left,depth+1),dfs(root->right,depth+1));
        }
    };

  • 相关阅读:
    接口问题
    鉴权 授权 验签
    adb常用命令
    cookie session
    常见http返回状态码
    Linux下mysql数据库的命令
    Linux课堂笔记--第九天
    Linux课堂随笔 -第八天
    Linux课堂笔记-第七天
    Linux课堂随笔-第六天
  • 原文地址:https://www.cnblogs.com/lancelee98/p/13040114.html
Copyright © 2011-2022 走看看