/*输入一棵二叉树的根节点,求该树的深度。
从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
*/
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;
}
}
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def calculateDepth(self, root):
"""
:type root: Optional[TreeNode]
:rtype: int
"""
if not root:
return 0
left = self.calculateDepth(root.left)
right = self.calculateDepth(root.right)
return max(left, right) + 1
class Solution {
public int calculateDepth(TreeNode root) {
if (null == root) {
return 0;
}
int left = calculateDepth(root.left) + 1;
int right = calculateDepth(root.right) + 1;
return Math.max(left,right);
}
}
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 calculateDepth(TreeNode* root) {
if(root nullptr){
return 0;
}
int left = calculateDepth(root->left);//左子树高度
int right = calculateDepth(root->right);
if(left > right) {
return left+1;
}else{
return right +1;
}
/
class Solution {
public:
int calculateDepth(TreeNode root) {
if(nullptr root) return 0;
int left = calculateDepth(root->left);
int right = calculateDepth(root->right);
return max(left,right) + 1;
}
};
时间复杂度:O(n);空间复杂度O(n)
class Solution {
public int calculateDepth(TreeNode root) {
if(root null)
return 0;
else
return(Math.max(calculateDepth(root.left),calculateDepth(root.right))+1);
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
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 {
int depth = 0;
public int calculateDepth(TreeNode root) {
if(root==null){
return 0;
}
int leftDepth = calculateDepth(root.left);
int rightDepth = calculateDepth(root.right);
return leftDepth>rightDepth?leftDepth+1:rightDepth+1;
}
}
思路:树是一个递归类型的数据结构,所以树的深度=子树的深度(左右子树谁更深就是谁)+1;因此递归调用方法,获取子树的高度,然后进行左右子树的高度对比,谁深就返回谁,如果某一颗子树为空,那这个子树的深度就为0;
空间复杂度:主要是递归深度,O(length)
共以下 38 个回答
第n次打卡
时间复杂度:O(n)
空间复杂度:O(1)
day6第一题
class Solution {
public:
int calculateDepth(TreeNode* root) {
if(rootnullptr) return 0;
};
打卡
思路:递归左右子树深度,当前节点深度等于左右子树的大的
代码:
二叉树的深度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;
}
时间复杂度O(n)
空间复杂度O(n)
时间复杂度$O(n)$,空间复杂度$O(n)$
时间复杂度O(N),空间复杂度O(N)
class Solution {
public:
int calculateDepth(TreeNode* root) {
int val=1;
if(!root)
return 0;
if(root->leftnullptr && root->rightnullptr)
return val;
int val1=1,val2=1;
if(root->left!=nullptr) val1+=calculateDepth(root->left);
if(root->right!=nullptr) val2+=calculateDepth(root->right);
return (val1>val2)?val1:val2;
}
};
二叉树的深度
class Solution{
public int calculateDepth(TreeNode root){
if(root null) return 0;
return Math.max(calculateDepth(root.left),calculateDepth(root.right)) + 1;
}
}
class Solution {
public:
//后序遍历
int calculateDepth(TreeNode* root) {
if(root nullptr){
return 0;
}
int left = calculateDepth(root->left);//左子树高度
int right = calculateDepth(root->right);
if(left > right) {
return left+1;
}else{
return right +1;
}
};
时间复杂度:O(n);空间复杂度O(n)
/
class Solution {
public:
int calculateDepth(TreeNode root) {
if(nullptr root) return 0;
int left = calculateDepth(root->left);
int right = calculateDepth(root->right);
return max(left,right) + 1;
}
};
时间复杂度:O(n);空间复杂度O(n)
java
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)
递归法,时间复杂度O(n),空间复杂度O(n)
递归深搜,时间复杂度 O(n), 空间复杂度 O(n)
class Solution {
public int calculateDepth(TreeNode root) {
if(root null)
return 0;
else
return(Math.max(calculateDepth(root.left),calculateDepth(root.right))+1);
}
//递归 时空复杂度均为On
public int calculateDepth(TreeNode root) {
if(rootnull) return 0;
return Math.max(calculateDepth(root.left),calculateDepth(root.right))+1;
}
思路:二叉树的深度=左右子树的深度+1 递归即可
打卡
https://leetcode.cn/problems/er-cha-shu-de-shen-du-lcof/solutions/2672345/ceng-xu-bian-li-ji-suan-er-cha-shu-shen-b2idc
打卡
思路:使用队列记录每一层的非空节点,然后逐层遍历
“`c++ que;
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int calculateDepth(TreeNode* root) {
if (root == nullptr) return 0;
queue
que.push(root);
int res = 0;
while (!que.empty()) {
int size = que.size();
while (size–) {
TreeNode* cur = que.front();
que.pop();
if (cur->left) que.push(cur->left);
if (cur->right) que.push(cur->right);
}
res++;
}
return res;
}
};
“`
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
时间复杂度O(N)
空间复杂度O(N)
后续遍历二叉树
class Solution {
public:
int calculateDepth(TreeNode* root) {
if (root nullptr) return 0;
return max(calculateDepth(root->left), calculateDepth(root->right)) + 1;
}
};后序遍历,时空复杂度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)
“`C++
class Solution {
public:
int calculateDepth(TreeNode* root) {
if (root == nullptr)
{
return 0;
}
};
“`
class Solution {
public int calculateDepth(TreeNode root) {
if(rootnull)
return 0;
return 1+Math.max(calculateDepth(root.left),calculateDepth(root.right));
}
}
时间复杂度O(n)
class Solution {
public:
int calculateDepth(TreeNode* root) {
if(root nullptr) return 0;
return 1 + max(calculateDepth(root->left),calculateDepth(root->right));
}
};
时间O(n),空间O(logn)
时空复杂度:O(n)
递归的方式做,时空复杂度:O(n)