[LeetCode-104] Maximum Depth of Binary Tree(二叉树最大深度)
来源:互联网 发布:cheat engine 6.3 mac 编辑:程序博客网 时间:2024/04/29 22:27
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.
【分析】
本题就是求解二叉树的深度:
递归解法:
(1)如果二叉树为空,二叉树的深度为0
(2)如果二叉树不为空,二叉树的深度 = max(左子树深度, 右子树深度) + 1
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int maxDepth(struct TreeNode* root) { if(root == NULL) // 递归出口 return 0; int depthLeft = maxDepth(root->left); int depthRight = maxDepth(root->right); return depthLeft > depthRight ? (depthLeft + 1) : (depthRight + 1); }
1 0
- 【LeetCode】Maximum Depth of Binary Tree (二叉树最大深度)
- Leetcode Maximum Depth of Binary Tree 二叉树最大深度
- [LeetCode-104] Maximum Depth of Binary Tree(二叉树最大深度)
- LeetCode 104 Maximum Depth of Binary Tree(二叉树的最大深度)
- LeetCode-104 Maximum Depth of Binary Tree 二叉树的最大深度
- LeetCode OJ 之 Maximum Depth of Binary Tree (二叉树的最大深度)
- [leetcode]Maximum Depth of Binary Tree (求二叉树的最大深度 C语言)
- leetCode 104.Maximum Depth of Binary Tree(二叉树最大深度) 解题思路和方法
- leetcode系列(28)Maximum Depth of Binary Tree 求二叉树的最大深度
- Leetcode 104.Maximum Depth of Binary Tree (二叉树的最大深度)
- LeetCode--Maximum Depth of Binary Tree (检索二叉树的最大深度)Python
- [leetcode]Maximum Depth of Binary Tree(二叉树最大深度、Depth-first search)
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- maximum-depth-of-binary-tree(二叉树的最大深度)
- 黑马程序员----oc基础笔记----类和对象
- 如何在Linux下使用Markdown进行文档工作
- 华为OJ字符串反转
- 黑马程序员--IO流(Properties、打印流 、文件切割与合并)
- PHP之——Boolean类型的判定
- [LeetCode-104] Maximum Depth of Binary Tree(二叉树最大深度)
- 自编数据库连接池应用程序
- hdu2199
- xutils中configCurrentHttpCacheExpiry()的用法
- sheepdog概述(安装及简单使用)
- hdu1976
- [Socket] Connect失败,显示Connection refused
- 用Hello World校验Docker的安装
- 安卓开发获取屏幕像素和密度