Maximum Depth of Binary Tree(二叉树最大深度)
2017-09-16 10:44
399 查看
来源:https://leetcode.com/problems/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.
1. 深度遍历,递归
Java
Python
2. 广度遍历(逐层遍历),使用队列
Java
Python
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.
1. 深度遍历,递归
Java
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 public int maxDepth(TreeNode root) { 12 if(root == null) { 13 return 0; 14 } 15 int leftDepth = maxDepth(root.left) + 1; 16 int rightDepth = maxDepth(root.right) + 1; 17 return leftDepth > rightDepth ? leftDepth : rightDepth; 18 } 19 }
Python
1 # Definition for a binary tree node. 2 # class TreeNode(object): 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution(object): 9 def maxDepth(self, root): 10 """ 11 :type root: TreeNode 12 :rtype: int 13 """ 14 if not root: 15 return 0 16 left_depth = self.maxDepth(root.left) + 1 17 right_depth = self.maxDepth(root.right) + 1 18 return left_depth if left_depth>right_depth else right_depth
2. 广度遍历(逐层遍历),使用队列
Java
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 public int maxDepth(TreeNode root) { 12 Queue<TreeNode> queue = new LinkedList<TreeNode>(); 13 int depth = -1; 14 queue.offer(root); 15 TreeNode node = null; 16 int queueSize = 0; 17 while(!queue.isEmpty()) { 18 queueSize = queue.size(); 19 while(queueSize-- > 0) { 20 node = queue.poll(); 21 if(node == null) { 22 continue; 23 } 24 queue.offer(node.left); 25 queue.offer(node.right); 26 } 27 depth += 1; 28 } 29 return depth; 30 } 31 }
Python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def maxDepth(self, root): """ :type root: TreeNode :rtype: int """ queue = [root] depth = -1 while queue: depth += 1 queue = [kid for node in queue if node for kid in (node.left, node.right)] return depth
相关文章推荐
- Maximum Depth of Binary Tree(二叉树的最大深度)
- LeetCode 104 Maximum Depth of Binary Tree(二叉树的最大深度)
- LinCode Maximum Depth of Binary Tree 二叉树的最大深度
- 【二叉树的递归】02二叉树的最大深度【Maximum Depth of Binary Tree】
- 【LeetCode】 Maximum Depth of Binary Tree 二叉树的最大深度 - Easy
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- 【LeetCode笔记】Maximum Depth of Binary Tree 二叉树最大深度 递归&非递归
- Leetcode Maximum Depth of Binary Tree 二叉树最大深度
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | 深度优先与广度优先深入理解
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- [Leetcode] Maximum depth of binary tree二叉树的最大深度
- lintcode maximum-depth-of-binary-tree 二叉树的最大深度
- 【LeetCode】 Maximum Depth of Binary Tree 二叉树的最大深度
- LeetCode OJ 之 Maximum Depth of Binary Tree (二叉树的最大深度)
- LeetCode OJ:Maximum Depth of Binary Tree(二叉树最大深度)
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- [leetcode]Maximum Depth of Binary Tree (求二叉树的最大深度 C语言)
- LeetCode--Maximum Depth of Binary Tree (检索二叉树的最大深度)Python
- Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 [java]