Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Have you met this question in a real interview?
Yes
Example
Given a binary tree as follow:
1
/ \
2 3
/ \
4 5
The minimum depth is 2
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int minDepth(TreeNode root) {
// write your code here
if(root == null) return 0;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
int level = 0;
while(!queue.isEmpty()){
Queue<TreeNode> nextQueue = new LinkedList<TreeNode>();
level++;
while(!queue.isEmpty()){
TreeNode node = queue.poll();
if(node.left == null && node.right == null)
return level;
if(node.left != null) nextQueue.offer(node.left);
if(node.right != null) nextQueue.offer(node.right);
}
queue = nextQueue;
}
return level;
}
}
Solution 2:
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int minDepth(TreeNode root) {
// write your code here
if(root == null) return 0;
return getMin(root);
}
public int getMin(TreeNode root){
if(root == null) return Integer.MAX_VALUE;
if(root.left == null && root.right == null) return 1;
return Math.min(getMin(root.left), getMin(root.right)) + 1 ;
}
}
Related Problems Expand
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int maxDepth(TreeNode root) {
// write your code here
if(root == null) return 0;
if(root.left == null && root.right == null) return 1;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
solution 2:
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int maxDepth(TreeNode root) {
// write your code here
if(root == null) return 0;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
int level = 0;
while(!queue.isEmpty()){
Queue<TreeNode> nextQueue = new LinkedList<TreeNode>();
level++;
while(!queue.isEmpty()){
TreeNode node = queue.poll();
if(node.left != null) nextQueue.offer(node.left);
if(node.right != null) nextQueue.offer(node.right);
}
queue = nextQueue;
}
return level;
}
}
Solution 2:
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int minDepth(TreeNode root) {
// write your code here
if(root == null) return 0;
return getMin(root);
}
public int getMin(TreeNode root){
if(root == null) return Integer.MAX_VALUE;
if(root.left == null && root.right == null) return 1;
return Math.min(getMin(root.left), getMin(root.right)) + 1 ;
}
}
Maximum Depth of Binary Tree
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.
Have you met this question in a real interview?
Yes
Example
Tags Expand
Given a binary tree as follow:
1
/ \
2 3
/ \
4 5
The maximum depth is
3
.Related Problems Expand
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int maxDepth(TreeNode root) {
// write your code here
if(root == null) return 0;
if(root.left == null && root.right == null) return 1;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
solution 2:
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int maxDepth(TreeNode root) {
// write your code here
if(root == null) return 0;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
int level = 0;
while(!queue.isEmpty()){
Queue<TreeNode> nextQueue = new LinkedList<TreeNode>();
level++;
while(!queue.isEmpty()){
TreeNode node = queue.poll();
if(node.left != null) nextQueue.offer(node.left);
if(node.right != null) nextQueue.offer(node.right);
}
queue = nextQueue;
}
return level;
}
}
No comments:
Post a Comment