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:
def maxDepth(self, root: TreeNode) -> int:
if not root:
# 没有左右孩子,到叶子节点了
return 0
left = self.maxDepth(root.left)
right = self.maxDepth(root.right)
return 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;
};
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
left := maxDepth(root.Left)
right := maxDepth(root.Right)
return Max(left, right) + 1
}
func Max(x, y int) int {
if x < y {
return y
}
return x
}
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
left := maxDepth(root.Left) //遍历左子树
right := maxDepth(root.Right) //遍历右子树
return max(left, right) + 1
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
/**
* 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 maxInt(maxDepth(root.Left), maxDepth(root.Right)) + 1
}
func maxInt(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 max(left, right) + 1;
}
};
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if(not root):
return 0
left = self.maxDepth(root.left)
right = self.maxDepth(root.right)
return left +1 if left > right else 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,right)+1;
}
}
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int res = 0;
Queue queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int count = queue.size();
for (int i = 0; i < count; i ++) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
res ++;
}
return res;
}
}
class Solution {
public int maxDepth(TreeNode root) {
if(root == null)
return 0;
int L = maxDepth(root.left);
int R = maxDepth(root.right);
return L>R ?L+1 : R+1;
}
}
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr)
return 0;
int l = maxDepth(root->left);
int r = maxDepth(root->right);
return std::max(l, r) + 1;
}
};
#方法1:
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if not root: return 0
num,res = [root],0
while num:
tem =[]
for k in num:
if k.left: tem.append(k.left)
if k.right:tem.append(k.right)
num = tem
res+=1
return res
方法2:递归
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if not root: return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if root is None :
return 0
else:
l = self.maxDepth(root.left)
r = self.maxDepth(root.right)
return max(l,r)+1
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==NULL)return 0;
else{
int left=maxDepth(root->left)+1;
int right=maxDepth(root->right)+1;
return left>right?left:right;
}
}
};
/**
递归
时间复杂度:O(n),其中 n 为二叉树节点的个数。每个节点在递归中只被遍历一次。
空间复杂度:O(height),其中height 表示二叉树的高度。递归函数需要栈空间,而栈空间取决于递归的深度,因此空间复杂度等价于二叉树的高度
*/
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;
Deque<TreeNode> queue = new LinkedList<>();
int depth = 0;
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0;i < size;i++){
TreeNode cur = queue.poll();
if(cur.left != null) queue.offer(cur.left);
if(cur.right != null) queue.offer(cur.right);
}
depth++;
}
return depth;
}
}
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int a = maxDepth(root.left)+1;
int b = maxDepth(root.right)+1;
return Math.max(a,b);
}
}
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if not root:
return 0
stack = [root]
res = 0
while stack:
res += 1
for i in range(len(stack)):
node = stack.pop(0)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
return res
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
//返回当前节点的左子树和右子树的深度最大值再加1
return Math.max(left,right)+1;
}
}
1、利用递归 分别计算左右子树的深度 然后+1 =树的深度
”’java
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public int TreeDepth(TreeNode root) {
if(rootnull) return 0;
int ld=TreeDepth(root.left);
int rd=TreeDepth(root.right);
return Math.max(ld,rd)+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;
}
}
共以下 61 个回答
递归实现
递归题目,优先考虑当前函数的功能能否为自己所服用,可以,就形成递归。
C++递归
“`c++
/**
* 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;
if(root != NULL &&(root->left == NULL && root->right == NULL)) return 1;
return max(maxDepth(root->right)+1, maxDepth(root->left)+1);
}
};
“`
思路:递归
● 时间复杂度:O(n)
● 空间复杂度: O(n)
时间O(n)空间O(n)
时间复杂度:O(n),其中 nn 为二叉树节点的个数。每个节点在递归中只被遍历一次。
空间复杂度:O(height),其中height 表示二叉树的高度。递归函数需要栈空间,而栈空间取决于递归的深度,因此空间复杂度等价于二叉树的高度。
时间O(N) 空间 O(N)
dfs或bfs均可
递归
后序遍历,函数功能是返回节点深度,关系式当前节点深度等于左右子树深度最大值+1,base是节点为null时深度为0。
使用树的后续遍历,然后递归执行
时间复杂度on
空间复杂度on
时间复杂度O(n)
空间复杂度O(n)
时间复杂度O(logN)和O(n)之间
空间复杂度O(n)
DFS
时间复杂度O(N)
空间复杂度O(height) height为二叉树的高度
时间复杂度On
空间复杂度O(height)
时间复杂度On
空间复杂度O(height)
思路一:递归。递归的终止条件是当节点等于null,返回0。递归函数的返回参数类型是int型,因为深度是一个数字,注意二叉树的深度是从上往下算,高度是从下往上算。递归函数的返回值为二叉树左子树、右子树两个深度的最大值。时间复杂度为O(N),N为树的节点个数,空间复杂度为O(height),递归需要栈空间,递归多少次取决于树的高度。
思路二:利用层次遍历的思想,求有多少层,深度就为多少。时间复杂度O(N),空间复杂度取决于队列存储的元素个数。用了一个队列,但队列大部分情况下不会存储树中全部节点。
时间:O(n)
空间:O(n)
递归或者层次遍历
后序递归遍历,左右子树高者为父节点高度
时间O(n),空间O(1)
思路:用递归的方式来进行深度优先搜索
时间复杂度:O(n)
空间复杂度:O(n)
学习新思路,做好每道题
递归思路还是不好想,用的是左右结点存储遍历,返回深度 时间复杂度O(n),空间复杂度O(n)
1、运用递归求左子树和右子树的二叉树深度,求最大值并+1
时间:O(n)
递归版本
使用递归的思路
//思路:递归思想,三部曲
额
思路:深度优先,自底向上。时间复杂度O(n),空间复杂度O(n)
深度优先:采用递归的思想,树的深度为1+max(左子树深度, 右子树深度)
深搜
时间O(n)
空间O(n)
时间复杂度:O(n)
空间复杂度:O(n)
方法1:深度优先,使用递归思想,树的深度=1+max(左子树的深度,右子树的深度),时间复杂度:O(n),空间复杂度O(height)
方法2:广度优先,使用队列,找到每层的所有节点,当当前层的节点数为0时,结束循环,返回计数深度,时间复杂度O(n),空间复杂度O(n)
时间复杂度:O(n)
空间复杂度:O(n)
递归
层次遍历
递归思路,定义两个变量分别记录左子树和右子树,
时间O(n)
空间O(n)
时间复杂度 O(n)
树的遍历方式总体分为两类:深度优先搜索(DFS)、广度优先搜索(BFS);
常见的 DFS : 先序遍历、中序遍历、后序遍历;
常见的 BFS : 层序遍历(即按层遍历)
时间、空间复杂度O(n)
时间复杂度为O(n),空间复杂度为O(lgn)。
1、利用递归 分别计算左右子树的深度 然后+1 =树的深度
”’java
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
}
*/
public class Solution {
public int TreeDepth(TreeNode root) {
if(rootnull) return 0;
int ld=TreeDepth(root.left);
int rd=TreeDepth(root.right);
return Math.max(ld,rd)+1;
}
”’
时间复杂度:
时间:O(n),空间:(height)
递归:计算左右子树中的最大深度,加上根节点,即为该树的最大深度,时间O(n),空间O(n)
//递归写法
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root) return 0;
return 1+max(maxDepth(root->left),maxDepth(root->right));
};