leetCode练习(104)
2016-10-29 12:55
309 查看
题目:Maximum Depth of Binary Tree
难度:easy
问题描述:
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.
解题思路:
求二叉树的层数,使用dfs深度搜索,层数=1+max(左子树层数,右子树层数)。
代码如下:
难度:easy
问题描述:
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.
解题思路:
求二叉树的层数,使用dfs深度搜索,层数=1+max(左子树层数,右子树层数)。
代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int maxDepth(TreeNode root) { return bfs(root); } private int bfs(TreeNode root){ if(root==null) return 0; return 1+Math.max(bfs(root.left),bfs(root.right)); } }
相关文章推荐
- LeetCode 104 Maximum Depth of Binary Tree
- leetcode练习(566)
- LeetCode编程练习 - Plus One学习心得
- leetcode练习题目
- LeetCode编程练习 - Happy Number学习心得
- leetCode练习(36)
- LeetCode练习一:Single Number
- [LeetCode]104. Basic Calculator II基本计算器
- leetCode练习(39)
- LeetCode 104 Maximum Depth of Binary Tree
- LeetCode Online Judge 题目C# 练习 - Longest Palindromic Substring
- 【LEETCODE】104-Maximum Depth of Binary Tree
- LeetCode Online Judge 题目C# 练习 - N-Queens
- LeetCode Online Judge 题目C# 练习 - Plus One
- [LeetCode-104]Maximum Depth of Binary Tree (java)
- LeetCode Online Judge 题目C# 练习 - Rotate List
- LeetCode Online Judge 题目C# 练习 - Search a 2D Matrix
- LeetCode Online Judge 题目C# 练习 - Sparial Matrix II
- 【LeetCode014-015算法/编程练习C++】最长共同前缀,3Sum(和为0) //用到了map的自动排序
- LeetCode Online Judge 题目C# 练习 - Valid Number