LeetCode 刷题之二:寻找二叉树的最大深度
来源:互联网 发布:淘宝微淘的好处 编辑:程序博客网 时间:2024/05/16 15:48
题目为:
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 * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int maxDepth(TreeNode root) { if(root == null) return 0; else if(root.left == null && root.right == null) return 1; else { int leftDepth= maxDepth(root.left); int rightDepth = maxDepth(root.right); if(leftDepth > rightDepth) return leftDepth +1; else return rightDepth +1; } }}
1 0
- LeetCode 刷题之二:寻找二叉树的最大深度
- LeetCode:寻找二叉树的最小深度
- LeetCode系列之二叉树最大深度求解问题 C++
- LeetCode OJ 之 Maximum Depth of Binary Tree (二叉树的最大深度)
- 二叉树的最大深度算法面试题-leetcode学习之旅(3)
- LeetCode—二叉树的最大最小深度
- LeetCode 刷题3 -- 二叉树的最大深度
- 二叉树的最大深度(leetcode-104)
- leetcode 104求二叉树的最大深度
- [LeetCode] 104 二叉树最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 二叉树的最大深度
- 解决GridView动态设置宽高导致第一个item点击不响应或显示不正常问题
- 新秩序建立的痛苦性
- x210 DNW配置问题 win7系统
- Shell循环列出目录下所有文件或查找目录下包含指定字符串的文件
- Javascript+Canvas实现吸尘器模型
- LeetCode 刷题之二:寻找二叉树的最大深度
- Phoenix常见问题解答
- Android 在Eclipse中使用Ant Build构建项目时出现的"crunch"
- Unity3D学习之(高级查询和聚合函数)
- 第16周项目1-数组的排序
- stm32时钟分析
- LeetCode刷题之三:判断两个二叉树是否相同
- 提升C++输入输出性能的三个方法
- UVA - 11059 Maximum Product