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 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;
}
public static void main(String[] args) {
TreeNode node1 = new TreeNode(3);
TreeNode node2 = new TreeNode(9);
TreeNode node3 = new TreeNode(20);
TreeNode node4 = new TreeNode(15);
TreeNode node5 = new TreeNode(7);
node1.left = node2;
node1.right = node3;
node3.left = node4;
node3.right = node5;
Solution sol = new Solution();
int i = sol.maxDepth(node1);
System.out.println(i);
}
}
class Solution {
public:
int depth = 0;
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.
* 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;
}
}
共以下 29 个回答
递归
时间复杂度:o(n)
空间复杂度:o(n)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode root) {
if(root NULL)
{
return 0;
}
return max(maxDepth(root->right), maxDepth(root->left)) + 1;
}
private:
int depth = 0;
};
时间复杂度:O(n),空间复杂度:O(n)
struct TreeNode{
int val;
TreeNode * left;
TreeNode * right;
TreeNode(int x):val(x),left(NULL),right(NULL){}
};
int maxDepth(TreeNode* dept)
{
if(dept NULL)
{
return 0;
}
int leftDepth = maxDepth(dept->left);//左深度
int rightDepth = maxDepth(dept->right);//右深度
}//时间复杂度O(n),空间复杂度O(n)
if(rootnull)
return 0;
int left = maxDepth(root->left);//先左遍历
int right = maxDepth(root->right);再右遍历
return max(left,right)+1;//比较左叉树和右叉树的中的大的加一为整个的
时间 On 空间On
打卡
class Solution {
public:
int maxDepth(TreeNode* root) {
}
};
时间复杂度:O(n )
空间复杂度:O(n )
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
// 深度优先遍历,时间复杂度o(n), 空间复杂度o(logn)
// int maxDepth(TreeNode root) {
// if (root nullptr) return 0;
// return max(maxDepth(root->left), maxDepth(root->right)) + 1;
// }
};
设n为节点个数
空间复杂度o(logn),时间复杂度o(n)
时间复杂度:O(n)
空间复杂度:O(height)
class Solution {
public:
int maxDepth(TreeNode* root) {
if(rootNULL) return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
};
直接写函数
”’java
if(root null) {
return 0;
}
//root空0;左右最大+1;
int maxDepth(struct TreeNode* root){
if(root NULL){
return 0;
}
return fmax(maxDepth(root->left),maxDepth(root->right))+1;
}
打卡
時間複雜度 O(n) n為二叉樹中節點數量 空間複雜度 O(height) height為二叉樹深度
Python 递归
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if not root:
return 0
自己写对的递归!开心!
“`python
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if not root: return 0
“`python
取左子树和右子树最大深度,递归。
1、队列实现层序遍历
递归
class Solution {
public int maxDepth(TreeNode root) {
if(root null){
return 0;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
Consider the self.parameter and enter in/out 在
递归算法
深度优先搜索
// 时间O(n),空间O(log n)
深度优先搜索
+ 时间复杂度O(N), N表示二叉树总节点数;
+ 空间复杂度O(Height), Height表示二叉树的高度。
递归
递归