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;
}
};
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+1,right+1);
}
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if root == nil{
return 0
}
var lh int = maxDepth(root.Left)
var rh int = maxDepth(root.Right)
return int(math.Max(float64(lh),float64(rh))) + 1 //选最深的
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
return max(maxDepth(root.Left), maxDepth(root.Right)) + 1
}
func max(a,b int) int {
if a > b {
return a
}
return b
}
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;
}
}
/**
* 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){
return 0;
}
int left = maxDepth(root -> left);
int right = maxDepth(root -> right);
return max(left,right)+1;
}
};
“`java
class Solution {
//经典递归
int max =0;
public int maxDepth(TreeNode root) {
if(root==null)
{
return 0;
}
int left =maxDepth(root.left);
int right =maxDepth(root.right);
if(left>right)
{
max=left;
}else
{
max=right;
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
maxDepth := 0
var traverse func(*TreeNode, int)
traverse = func (root *TreeNode, depth int) {
if root == nil {
return
}
depth++
//如果是叶子节点,更新最大深度
if root.Right == nil && root.Left == nil {
maxDepth = max(maxDepth, depth)
}
traverse(root.Left, depth)
traverse(root.Right, depth)
}
traverse(root, 0)
return maxDepth
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
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) {
return travel(root);
}
public int travel(TreeNode root){
if(root == null) return 0;
int leftDepth = travel(root.left);
int rightDepth = travel(root.right);
return Math.max(leftDepth, rightDepth) + 1;//返回左右子树最大高度+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;
}
}
/**
* 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;
}
}
/**
* 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 1+Math.max(left,right);
}
}
class Solution {
public int maxDepth(TreeNode root) {
return getDepth(root);
}
public int getDepth(TreeNode root) {
if(root == null) return 0;
int leftDepth = getDepth(root.left);
int rightDepth = getDepth(root.right);
return Math.max(leftDepth,rightDepth) + 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;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left, right) + 1;
}
}
共以下 40 个回答
递归:
1. 明确递归函数作用
2. 找递归函数退出边界条件
3. 找函数关系缩小函数范围
迭代,时间复杂度O(n) n是节点数,空间复杂度O(height)
递归遍历左子树和右子树
时间O(N) 空间O(N)
递归
通过
时间复杂度:O(N)
空间复杂度:O(1)
/**中文
* 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 nullptr) return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
简单题
二叉树深度
用队列代替递归方法
/**
* 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) {
return 0;
}
int left = maxDepth(root->left); // 递归
int right = maxDepth(root->right); // 递归
};
层序遍历了
两种方法,一种是用回溯的方法,一种是遍历二叉树
”’java
class Solution {
public int maxDepth(TreeNode root) {
if(root null){
return 0;
}
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return leftDepth > rightDepth ? leftDepth+1:rightDepth+1;
}
}
”’
时间复杂度O(n)
空间复杂度O(n)
代码
时间复杂度:O(n)
空间复杂度:O(height) height是树的高度
“`java
class Solution {
//经典递归
int max =0;
public int maxDepth(TreeNode root) {
if(root==null)
{
return 0;
}
int left =maxDepth(root.left);
int right =maxDepth(root.right);
if(left>right)
{
max=left;
}else
{
max=right;
}
}
深度优先
时间复杂度O(n)空间复杂度O(1)时间:o(n)
c++ 代码层次遍历
class Solution {
public:
queue<TreeNode> inQueue;
int maxDepth(TreeNode root)
{
if(!root) return 0;
inQueue.push(root);
int count=0;
};
Day6:下面是代码
“`c++
//自顶向下
class Solution {
public:
int ans=0;
void dfs(TreeNode* T,int deep)
{
if(T = =NULL)
{
ans =max(ans , deep);
return;
}
dfs(T->left,deep+1);
dfs(T->right,deep+1);
}
int maxDepth(TreeNode* root) {
dfs(root,0);
return ans;
}
};
代码
时间复杂度:O(N)
空间复杂度:O(1)
时间复杂度:O(N);
空间复杂度:O(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;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left, right) + 1;
}
}