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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for binary tree | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
//new solution write at 3/5/2014 | |
public class Solution { | |
public int minDepth(TreeNode root) { | |
if (root==null){ | |
return 0; | |
} | |
if (root.left==null && root.right==null){ | |
return 1; | |
} | |
if (root.left!=null && root.right!=null){ | |
return 1+Math.min(minDepth(root.left), minDepth(root.right)); | |
} | |
if (root.left==null){ | |
return 1+minDepth(root.right); | |
} | |
return 1+minDepth(root.left); | |
} | |
} | |
"Be care of that is the current root is empty the already added depth should -1 and return " | |
"Time Complexity is O(n)" | |
public class Solution { | |
public int minDepth(TreeNode root) { | |
if (root == null){ | |
return 0; | |
} | |
else if (root.left==null && root.right==null){ | |
return 1; | |
} | |
return minDepthHelper(root, 1); | |
} | |
public int minDepthHelper(TreeNode root, int depth){ | |
if (root==null){ | |
// if the current root is null, then the depth should -1 and return; | |
return depth-1; | |
} | |
int left=minDepthHelper(root.left, depth+1); | |
int right=minDepthHelper(root.right, depth+1); | |
// if left ==depth mean the left node is null, so return the right | |
if (left==depth){ | |
return right; | |
} | |
// if rigth==depth mean the right node is null, so return left | |
if (right==depth){ | |
return left; | |
} | |
return Math.min(left,right); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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. | |
""" | |
# Definition for a binary tree node | |
# class TreeNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
# Solution 1 | |
class Solution: | |
def minDepth(self, root): | |
if root==None: | |
return 0 | |
if root.left==None and root.right==None: | |
return 1 | |
depth=1 | |
return self.getMinDepth(root, depth) | |
def getMinDepth(self, root, depth): | |
if root==None: | |
return depth-1 | |
leftDepth=self.getMinDepth(root.left, depth+1) | |
rightDepth=self.getMinDepth(root.right, depth+1) | |
# left side depth equal to current depth mean left side is None, so just rightDepth | |
if leftDepth==depth: | |
return rightDepth | |
# same reason with above | |
if rightDepth==depth: | |
return leftDepth | |
# not None for both side, we return the smalles one | |
return min(leftDepth, rightDepth) | |
# Solution 2 | |
class Solution: | |
# @param root, a tree node | |
# @return an integer | |
def minDepth(self, root): | |
if root==None: | |
return 0 | |
if root.left==None and root.right==None: | |
return 1 | |
if root.left!=None and root.right!=None: | |
return min(self.minDepth(root.left), self.minDepth(root.right))+1 | |
if root.left!=None: | |
return self.minDepth(root.left)+1 | |
return self.minDepth(root.right)+1 |
No comments:
Post a Comment