Maximum Depth of Binary Tree leetcode java
2014-07-31 00:25
246 查看
题目:
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.
题解:
递归解法:
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int leftmax = maxDepth(root.left);
int rightmax = maxDepth(root.right);
return Math.max(leftmax, rightmax)+1;
}
非递归解法,参考codeganker(http://codeganker.blogspot.com/2014/02/maximum-depth-of-binary-tree-leetcode.html)的:
1 public int maxDepth(TreeNode root) {
2 if(root == null)
3 return 0;
4
5 int depth = 0;
6 LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
7 queue.add(root);
8 int curNum = 1; //num of nodes left in current level
9 int nextNum = 0; //num of nodes in next level
while(!queue.isEmpty()){
TreeNode n = queue.poll();
curNum--;
if(n.left!=null){
queue.add(n.left);
nextNum++;
}
if(n.right!=null){
queue.add(n.right);
nextNum++;
}
if(curNum == 0){
curNum = nextNum;
nextNum = 0;
depth++;
}
}
return depth;
}
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.
题解:
递归解法:
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int leftmax = maxDepth(root.left);
int rightmax = maxDepth(root.right);
return Math.max(leftmax, rightmax)+1;
}
非递归解法,参考codeganker(http://codeganker.blogspot.com/2014/02/maximum-depth-of-binary-tree-leetcode.html)的:
1 public int maxDepth(TreeNode root) {
2 if(root == null)
3 return 0;
4
5 int depth = 0;
6 LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
7 queue.add(root);
8 int curNum = 1; //num of nodes left in current level
9 int nextNum = 0; //num of nodes in next level
while(!queue.isEmpty()){
TreeNode n = queue.poll();
curNum--;
if(n.left!=null){
queue.add(n.left);
nextNum++;
}
if(n.right!=null){
queue.add(n.right);
nextNum++;
}
if(curNum == 0){
curNum = nextNum;
nextNum = 0;
depth++;
}
}
return depth;
}
相关文章推荐
- LeetCode 104 -Maximum Depth of Binary Tree ( JAVA )
- Java [Leetcode 104]Maximum Depth of Binary Tree
- LeetCode|Maximum Depth of Binary Tree-java
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- LeetCode之Maximum Depth of Binary Tree(Java版)
- Maximum Depth of Binary Tree | leetcode 104 【Java解题报告】
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- Java for LeetCode 104 Maximum Depth of Binary Tree
- [LeetCode][Java]Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree JAVA
- [LeetCode-104]Maximum Depth of Binary Tree (java)
- leetcode:Maximum Depth of Binary Tree 【Java】
- (java)leetcode Maximum Depth of Binary Tree
- [LeetCode][Java] Maximum Depth of Binary Tree
- [Leetcode] Maximum Depth of Binary Tree (Java)
- [LeetCode] Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- leetcode -- Maximum Depth of Binary Tree