“`java
两种解法,递归和层次遍历
class Solution {
public int calculateDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue queue = new LinkedList<>();
int res = 0;
queue.add(root);
while(!queue.isEmpty()) {
int n = queue.size();
for (int i = 0; i < n; i++) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
res++;
}
return res;
}
}
“`java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left,right)+1;
“`java
思路 递归
时间复杂度 0(n) 空间复杂度0(1)
class Solution{
public int maxDepth(TreeNode root){
if(root == null) return 0;
int left =maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left,right)+1;
}
var maxDepth = function(root) {
if(root === null) return 0;
let left = maxDepth(root.left);
let right = maxDepth(root.right);
return Math.max(left, right) + 1;
};
/**时间复杂度 O(n),空间复杂度O(h)
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if rootnil{
return 0
}
l:=maxDepth(root.Left)
r:=maxDepth(root.Right)
if l>=r{
return l+1
}else{
return r+1
}
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) {
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return (left > right ? left : right) + 1;
}
}
int maxDepth(struct TreeNode* root){
if(root == NULL){
return 0;
}
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return fmax(left,right) + 1;
}
c++递归
int getMaxdeep(TreeNode* temp)
{
if(tempNULL) return 0;
int leftdepth = getMaxdeep(temp->left);
int rightdepth = getMaxdeep(temp->right);
return 1+max(leftdepth,rightdepth);
}
int maxDepth(TreeNode* root) {
int deep = getMaxdeep(root);
return deep;
}
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int deep = 1;
return deep += Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null)return 0;
return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
}
}
class Solution {
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int m=maxDepth(root.left);
int n=maxDepth(root.right);
return m > n ? m+1 : n+1;
}
}
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
int left=maxDepth(root.left);
int right=maxDepth(root.right);
return Math.max(left,right)+1;
}
}
public:
int maxDepth(TreeNode* root){
if(!root){
return 0;
}
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return max(left,right) + 1;
}
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root == null)return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left,right)+1;
}
}
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL) return 0;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return max(left, right)+1;
}
};
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left, right) + 1;
}
//回溯法
class Solution1 {
int depth;
int maxDepth; // 记录最大深度
public int maxDepth(TreeNode root) {
traverse(root);
return maxDepth;
}
void traverse(TreeNode root) {
if (root == null) return;
depth++;
maxDepth = Math.max(maxDepth, depth);
traverse(root.left);
traverse(root.right);
depth--;
}
}
时间复杂度:O(n) 空间复杂度:O(1)
//动态规划法
class Solution2 {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
int leftMaxDepth = maxDepth(root.left);
int rightMaxDepth = maxDepth(root.right);
return Math.max(leftMaxDepth, rightMaxDepth) + 1;
}
}
//时间O(n),空间O(n)
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return left > right ? left + 1 : right + 1;
}
class Solution {
//时间复杂度O(n)
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left,right)+1;
}
}
public int maxDepth(TreeNode root) {
// 1.要求每一个结点的深度
//2.停止条件 root==null
//3.条件表达式 max(left,right)+!
if (root==null) return 0;
int left=maxDepth(root.left);
int right=maxDepth(root.right);
return Math.max(left,right)+1;
}
共以下 54 个回答
“`java queue = new LinkedList<>();
两种解法,递归和层次遍历
class Solution {
public int calculateDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue
int res = 0;
queue.add(root);
while(!queue.isEmpty()) {
int n = queue.size();
for (int i = 0; i < n; i++) { TreeNode node = queue.poll(); if (node.left != null) { queue.add(node.left); } if (node.right != null) { queue.add(node.right); } } res++; } return res; } }
“`java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left,right)+1;
}
递归三部曲:先明确函数作用,再确定递归结束条件,最后函数关系式
“`java
思路 递归
时间复杂度 0(n) 空间复杂度0(1)
class Solution{
public int maxDepth(TreeNode root){
if(root == null) return 0;
int left =maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left,right)+1;
}
思路:递归
【思路】简单递归
【复杂度】时间O(n),空间O(1)
C++递归实现
递归实现
这段代码定义了一个TreeNode类和一个Solution类。TreeNode类表示二叉树的节点,包含一个值属性和左右子节点属性。Solution类包含一个方法maxDepth,用于计算二叉树的最大深度。
maxDepth方法使用递归的方式计算二叉树的最大深度。首先判断根节点是否为空,如果为空则返回0。如果根节点不为空,则递归调用maxDepth方法计算左子树和右子树的最大深度,并取其中较大的值。最后将较大值加1,表示当前节点的深度,作为结果返回。
这段代码的时间复杂度为O(n),其中n为二叉树的节点数。因为每个节点只会被访问一次。空间复杂度为O(h),其中h为二叉树的高度,因为递归调用会使用到函数调用栈。
队列广搜
时间复杂度O(n)
空间复杂度O(n)
/**时间复杂度 O(n),空间复杂度O(h)
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if rootnil{
return 0
}
l:=maxDepth(root.Left)
r:=maxDepth(root.Right)
if l>=r{
return l+1
}else{
return r+1
}
}
时间复杂度o(h), 空间复杂度o(H)
时间复杂度O(n),空间复杂度O(n)
dfs
时间:O(n)
空间:O(height)
bfs
时间:O(n)
空间:O(n)
可以
时间复杂度O(n)
空间复杂度O(n)
时间复杂度O(n),空间复杂度O(n)
思路:递归
时间复杂度:O(n), 空间复杂度:O(n)
时间复杂度:O(N)
空间复杂度:O(logN)
public int maxDepth(TreeNode root){
if(root null) return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
}
时间复杂度o(n)
空间复杂度o(n)
c++递归
int getMaxdeep(TreeNode* temp)
{
if(tempNULL) return 0;
int leftdepth = getMaxdeep(temp->left);
int rightdepth = getMaxdeep(temp->right);
return 1+max(leftdepth,rightdepth);
}
int maxDepth(TreeNode* root) {
int deep = getMaxdeep(root);
return deep;
}
class Solution {
int depth = 0;
int res = 0;
public:
int maxDepth(TreeNode* root) {
traverse(root);
return res;
}
void traverse(TreeNode* root){
if(rootnullptr){
return;
}
depth++;
if(root->leftnullptr&&root->rightnullptr){
res = max(res, depth);
}
traverse(root->left);
traverse(root->right);
depth–;
}
};
时间复杂度O(N),空间复杂度O(1)
后序遍历DFS,时间复杂度 O(n)
时间复杂度为O(N)
空间复杂度为O(H),其中H是二叉树的高度
时间复杂度O(N)
空间复杂度O(树高)
复杂度
时间复杂度: $O(n)$
空间复杂度: $O(height)$
Code
递归时间复杂度O(n)空间复杂度为递归的深度
”’
思路:递归
C++
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root nullptr) return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
时间复杂度:二叉树中有n个节点,遍历整个二叉树,时间复杂度O(n);
空间复杂度:O(height),其中height 表示二叉树的高度。递归函数需要栈空间,而栈空间取决于递归的深度,因此空间复杂度等价于二叉树的高度。
”’
递归:时间复杂度O(n),空间复杂度O(n)
递归,以 NULL 为递归终点
时间复杂度O(n) 空间复杂度O(h)
时间复杂度:O(n)
空间复杂度:O(1)
依照层序遍历:
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 node = que.front();
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
};
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度:O(n)
时间复杂度:O(n)
空间复杂度:O(n)
1、递归思路
2、确定方法结构,确定返回值,确定缩小的递归的等式关系
时间复杂度:O(n)
空间复杂度:O(n)
思路:首先判断该节点是否为NULL,如果是,返回0递归结束条件
递归左子树,递归右子树,返回左右路径的最大值+1。
时间复杂度O(n);
空间复杂度O(height);
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度O(n),空间复杂度O(n)
时间复杂度O(nlogn),空间复杂度O(n)
思路
代码
class Solution {
//递归
public int maxDepth(TreeNode root) {
if(root null) return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
}
【时间O(logn),空间O(logn)】
递归计算每个节点
时间复杂度为n,空间复杂度为1
使用递归的方法,重要的是要理解每一个递归的功能,停止条件。