原题:
Given a binary tree, find its maximum depth.
=>给定一个二叉树,找到他的深度。
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
=>所谓的深度就是指从根节点到最远的叶节点之间的距离。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
}
};
晓东分析:
这其实是一个很基础的题目,稍微有点基础的同学应该都写过,所以也就不需要详细说明什么。这种题目使用递归的算法是最简单的,思路就是先求出左节点为根节点的二叉树的深度,再求出右节点为根节点的二叉树深度,然后看这两者谁大,大的那个加上1就是原来的二叉树的深度。
代码实现:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int left_depth = 0;
int right_depth = 0;
if(NULL == root) return 0;
left_depth = maxDepth(root->left);
right_depth = maxDepth(root->right);
return left_depth > right_depth ? left_depth + 1 : right_depth + 1;
}
};
执行结果:
38 / 38test cases passed.
|
Status:
Accepted
|
Runtime: 44 ms
|
执行时间还是可以接收的。
希望大家有更好的算法能够提出来,不甚感谢。
若您觉得该文章对您有帮助,请在下面用鼠标轻轻按一下“顶”,哈哈~~·
分享到:
相关推荐
BinaryTree-BinaryTree
BinaryTree-源码.rar
题目二叉树所有路径题解* Definition for a binary tree node.* function TreeNode(val) {* @para
2.binaryTree树 标准的二叉树。 实现了各种算法,增删查改等等
BinaryTree: 用于学习二叉树的Python库
Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树
先序二叉树(树状打印) 将二叉树以先序输出,并且图状打印
amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gzamoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz ...
leetcode 树节点二叉树路径 给定一棵二叉树,返回所有从根到...binaryTreePaths ( TreeNode root ) { List< String > result = new ArrayList<> (); if (root == null ){ return result; } dfs(root, " " ,resul
104.maximum-depth-of-binary-tree (二叉树的最大深度) 105.construct-binary-tree-from-preorder-and-inorder-traversal (从前序与中序遍历序列构造二叉树) 106.construct-binary-tree-from-inorder-and-postorder-...
binary-tree-master (3).zip
leetcode 数据结构题目中的答案,已经调试,直接运行,求二叉树的最小深度
二叉树 0098 Validate Binary Search Tree - Java Recursive - Java Iterative - Java Inorder 0099 Recover Binary Search Tree - Java Recursive 0101 Symmetric tree - Java Recursive - Java Iterative -...
线索二叉树 线索二叉树(Threaded Binary Tree)是对二叉树的一种优化,目的是使遍历二叉树的过程更加高效。
lisp语言写的二叉树程序,欢迎批评指正
二叉树的结构特征,以及链式存储结构的特点及程序设计方法
树节点二叉树最大路径和 执行 : /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } ...
实现二叉树的一些算法,超级经典的算法,各种函数功能的实现
树节点二叉树的直径 给定一棵二叉树,您需要计算树的直径长度。 二叉树的直径是树中任意两个节点之间最长路径的长度。 此路径可能会或可能不会通过根。 Example: Given a binary tree 1 / \ 2 3 / \ 4 5 Return 3, ...
开发环境 VS2019 由于C语言中没有引用 (&),所以这里使用的是C++,极大程度还原了课本中的代码。 若有不足,请批评指正!! 谢谢!!!...BinaryTree------二叉树 BiThrTree-------线索二叉树