LintCode: Maximum Depth of Binary Tree

简介:

C++

复制代码
 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14 public:
15     /**
16      * @param root: The root of binary tree.
17      * @return: An integer
18      */
19     int maxDepth(TreeNode *root) {
20         // write your code here
21         if (root == NULL) {
22             return 0;
23         }
24         return max(maxDepth(root->left), maxDepth(root->right)) + 1;
25     }
26 };
复制代码

 

本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5012506.html,如需转载请自行联系原作者

相关文章
Leetcode Minimum Depth of Binary Tree (面试题推荐)
计算树的最小深度 很简单的一道题,只需要遍历一次树,到叶子节点的时候计算一下深度和当前最小深度比较,保存最小值就行。 我在这用了一个全局变量 mindepth。总感觉我这代码写的不够简练,求更精简的方法。
47 0
LeetCode 104. Maximum Depth of Binary Tree
给定一颗二叉树,返回其最大深度. 注意:深度从1开始计数.
64 0
LeetCode 104. Maximum Depth of Binary Tree
LeetCode 111. Minimum Depth of Binary Tree
给定一棵二叉树,返回其最短深度. 题目要求是返回一颗二叉树从根节点到到所有叶节点中,深度最小的值.
78 0
LeetCode 111. Minimum Depth of Binary Tree
|
Go
LeetCode 124. Binary Tree Maximum Path Sum
给定一个非空二叉树,返回其最大路径和。 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。
68 0
LeetCode 124. Binary Tree Maximum Path Sum
LeetCode之Maximum Depth of Binary Tree
LeetCode之Maximum Depth of Binary Tree
99 0
|
Java
[LeetCode] Maximum Depth of Binary Tree
链接:https://leetcode.com/submissions/detail/136407355/难度:Easy题目:104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth.
966 0
|
Java
[LeetCode] Minimum Depth of Binary Tree
链接:https://leetcode.com/problems/minimum-depth-of-binary-tree/description/难度:Easy题目:111.
847 0