zoukankan      html  css  js  c++  java
  • 二叉树的最大深度---简单

    题目:

      给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

    示例:

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

        3
       / 
      9  20
        /  
       15   7

    返回它的最大深度 3 。

    思路:

      简单递归。

    /**
     * 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==nullptr) return 0;
            else
            {
                int left=1+maxDepth(root->left);
                int right=1+maxDepth(root->right);
                return left>right?left:right;
            }
        }
    };
  • 相关阅读:
    python的Collections 模块
    python模块
    python类
    python异常
    python文件处理
    python函数
    python字符串
    python数据结构
    python循环
    下载Google Play外国区APP技巧
  • 原文地址:https://www.cnblogs.com/manch1n/p/10338277.html
Copyright © 2011-2022 走看看