求给定二叉树的最大深度,
深度是指树的根节点到任一叶子节点路径上节点的数量。
最大深度是所有叶子节点的深度的最大值。
(注:叶子节点是指没有子节点的节点。)
数据范围:,树上每个节点的val满足
要求: 空间复杂度 ,时间复杂度
要求: 空间复杂度 ,时间复杂度
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 # @return int整型 # class Solution: def maxDepth(self , root ): # write code here # 递归 终止条件为到达叶子节点 if not root: return 0 left_len = self.maxDepth(root.left) right_len = self.maxDepth(root.right) return 1+max(left_len,right_len)
/* * 解法一: * 递归解法,非常简洁 */ public int maxDepth(TreeNode root) { if(root==null) return 0; return 1+Math.max(maxDepth(root.left), maxDepth(root.right)); } /* * 解法二: 使用queue进行层序遍历 */ public int maxDepth_1(TreeNode root) { if (root == null) return 0; Queue<TreeNode> queue = new LinkedList<TreeNode>(); int res = 0; queue.add(root); while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { TreeNode node = queue.poll(); if (node.left != null) queue.add(node.left); if (node.right != null) queue.add(node.right); } res++; } return res; }
public int maxDepth(TreeNode root){ if(root==null) return 0; int lDepth = maxDepth(root.left); int rDepth = maxDepth(root.right); return 1+(lDepth>rDepth?lDepth:rDepth); }
Leetcode#104. Maximum Depth of Binary Tree(二叉树的最大深度)
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
/**
* 递归
*
* @param root
* [@return](/profile/547241) */
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1);
}
/**
* 非递归,层次遍历
*
* @param root
* [@return](/profile/547241) */
public int maxDepth_2(TreeNode root) {
if (root == null) {
return 0;
}
int depth = 0;
int start = 0;
int end = 1;
Queue queue = new LinkedList();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode temp = queue.poll();
start++;
if (temp.left != null) {
queue.offer(temp.left);
}
if (temp.right != null) {
queue.offer(temp.right);
}
if (start == end) {
start = 0;
end = queue.size();
depth++;
}
}
return depth;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}
class Solution { public: /** * * @param root TreeNode类 * @return int整型 */ int depth = 0; int res = 0; int road = 0; void trasvel(TreeNode* root){ if(root == NULL) { res = max(res, road); return; } road++; trasvel(root->left); trasvel(root->right); road--; } int maxDepth(TreeNode* root) { // write code here trasvel(root); return res; } };
public class Solution { public int maxDepth (TreeNode root) { return root == null ? 0 : 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } }
public int maxDepth (TreeNode root) { if (root == null) { return 0; } return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); }
// 递归版本 class Solution{ public: int maxDepth(TreeNode* root) { return root == NULL ? 0 : max(maxDepth(root->left),maxDepth(root->right)) +1; } }; // 层序遍历bfs class Solution{ public: int maxDepth(TreeNode* root) { if(root == NULL) return 0; queue<TreeNode*> que; que.push(root); int depth = 0; while(!que.empty()) { int size = que.size(); depth++; for(int i = 0; i < size; ++i) { TreeNode* tmp = que.front(); que.pop(); if(tmp->left != NULL) que.push(tmp->left); if(tmp->right != NULL) que.push(tmp->right); } } return depth; } };