class Solution {
public:
int calculateDepth(TreeNode* root) {
if(!root)
{
return 0;
}
int left = calculateDepth(root->left);
int right = calculateDepth(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 # 一个节点null,深度为0 # 最底层返回的数值
if root.left and root.right: # 两个子树,深度为两者的最大深度+1
return max(self.maxDepth(root.left),self.maxDepth(root.right))+1 #向下递推公式
return self.maxDepth(root.left)+1 if root.left else self.maxDepth(root.right)+1 #只有一个子树,存在的子树深度+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 max(self.maxDepth(root.left)+1,self.maxDepth(root.right)+1) #递推公式
层序遍历
让每次全部pop一层的节点后,layer才加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
queue=[root]
layer=0
while queue:
length=len(queue) #增加了这个
for _ in range(length):
cur=queue.pop(0)
if cur.left: queue.append(cur.left)
if cur.right: queue.append(cur.right)
layer+=1 # 层数+1
return layer
class Solution {
public int calculateDepth(TreeNode root) {
if (root == null) {
return 0;
}
int left = calculateDepth(root.left);
int right = calculateDepth(root.right);
return Math.max(left, right) + 1;
}
}
class Solution {
public:
int calculateDepth(TreeNode* root) {
if(root == NULL) return 0;
int left = 0;
int right = 0;
if(root->left) left = calculateDepth(root->left);
if(root->right) right = calculateDepth(root->right);
return max(left, right) + 1;
}
};
class Solution {
public int calculateDepth(TreeNode root) {
// int ans = 0;
if(root == null ) return 0;
return Math.max(calculateDepth(root.left),calculateDepth(root.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 calculateDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
return max(self.calculateDepth(root.left), self.calculateDepth(root.right)) + 1
int depth;
public void dfs(TreeNode root, int cnt){
if(root==null)return;
depth=Math.max(depth,cnt);
dfs(root.left,cnt+1);
dfs(root.right,cnt+1);
}
public int calculateDepth(TreeNode root) {
if(root==null)return 0;
depth=0;
dfs(root,1);
return depth;
}
class Solution:
def calculateDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
left = self.calculateDepth(root.left)
right = self.calculateDepth(root.right)
# remember to +1 so that the value can increase.
return max(left, right) + 1
class Solution {
public int calculateDepth(TreeNode root) {
if (root == null) {
return 0;
}
int left = calculateDepth(root.left);
int right = calculateDepth(root.right);
return Math.max(left, right) + 1;
}
}
class Solution {
public int calculateDepth(TreeNode root) {
return maxDepth(root);
}
private 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;
}
}
共以下 27 个回答
递归
传递下去,值归回来
向下递推公式,直到返回底层值,又向上返回赋予值。
直接
层序遍历
让每次全部pop一层的节点后,layer才加1。
递归三步法:
函数的定义:f(root) :计算当前节点的深度
找关系式:f(root)=max(f(left),f(right))+1
初始值:root=null return 0
层序遍历
递归
1.传入根节点,传出当前根节点树的深度
2.出口返回深度int
3.递归函数:当前树深=左右子树最大深度+1
时间O(N), 空间O(N)
时间O(N),空间O(N)
递归
直接递归
“`cpp
class Solution {
public:
int calculateDepth(TreeNode* root) {
if(!root) return 0;
return max(calculateDepth(root->left),calculateDepth(root->right))+1;
}
};
直接进行简单的遍历
题解
递归走左边和右边,然后结果取max 并+1来保证计数器自增。
time O(n) space O(n) 递归过程中会有n个结果
时间复杂度O(n), 空间复杂度O(h)
时间o(n)
空间o(n)
“`java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int calculateDepth(TreeNode root) {
if(root == null) return 0;
return 1 + Math.max(calculateDepth(root.left), calculateDepth(root.right));
}
}
递归即可
思路:一种可以直接递归,终止条件是最后节点为null时返回0;
另一种是层序遍历BFS,但是需要记录每一层节点的数量,循环完一层的queue之后深度才加一
时间复杂度:O(n)
空间复杂度:O(n)
1.代码
2.复杂度分析
a.时间复杂度o(n);
b.空间复杂度o(n),递归过程会产生内存。
时间O(n) 空间O(n)
思路: 递归解决。 当前高度 = Math.max(子树高度) + 1
/ 时间复杂度为O(N),空间复杂度最坏为O(N)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int calculateDepth(TreeNode root) {
if(root null) {
return 0;
}
}
思路:递归计算节点的深度。
时间复杂度:O(n),每个节点被访问一次
空间复杂度:O(h),递归调用栈的深度取决于二叉树的高度。
递归
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public int calculateDepth(TreeNode root) {
if(root null){
return 0;
}
return Math.max(calculateDepth(root.left), calculateDepth(root.right)) + 1;
}
}
时间:O(n)
空间:O(n)
打卡,递归
时间复杂度O(n),空间复杂度O(n)