更新 0104. 二叉树最大深度

代码错误,更正代码,通过LeetCode代码检验。
This commit is contained in:
AronJudge
2022-07-12 11:36:26 +08:00
parent d8253df2cf
commit ba081f84a8

View File

@ -294,14 +294,13 @@ class solution {
/**
* 递归法
*/
public int maxdepth(treenode root) {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int leftdepth = maxdepth(root.left);
int rightdepth = maxdepth(root.right);
return math.max(leftdepth, rightdepth) + 1;
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return Math.max(leftDepth, rightDepth) + 1;
}
}
```
@ -311,23 +310,23 @@ class solution {
/**
* 迭代法,使用层序遍历
*/
public int maxdepth(treenode root) {
public int maxDepth(TreeNode root) {
if(root == null) {
return 0;
}
deque<treenode> deque = new linkedlist<>();
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root);
int depth = 0;
while (!deque.isempty()) {
while (!deque.isEmpty()) {
int size = deque.size();
depth++;
for (int i = 0; i < size; i++) {
treenode poll = deque.poll();
if (poll.left != null) {
deque.offer(poll.left);
TreeNode node = deque.poll();
if (node.left != null) {
deque.offer(node.left);
}
if (poll.right != null) {
deque.offer(poll.right);
if (node.right != null) {
deque.offer(node.right);
}
}
}