更新 0104. 二叉树最大深度

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

View File

@ -294,14 +294,13 @@ class solution {
/** /**
* 递归法 * 递归法
*/ */
public int maxdepth(treenode root) { public int maxDepth(TreeNode root) {
if (root == null) { if (root == null) {
return 0; return 0;
} }
int leftdepth = maxdepth(root.left); int leftDepth = maxDepth(root.left);
int rightdepth = maxdepth(root.right); int rightDepth = maxDepth(root.right);
return math.max(leftdepth, rightdepth) + 1; 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) { if(root == null) {
return 0; return 0;
} }
deque<treenode> deque = new linkedlist<>(); Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root); deque.offer(root);
int depth = 0; int depth = 0;
while (!deque.isempty()) { while (!deque.isEmpty()) {
int size = deque.size(); int size = deque.size();
depth++; depth++;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
treenode poll = deque.poll(); TreeNode node = deque.poll();
if (poll.left != null) { if (node.left != null) {
deque.offer(poll.left); deque.offer(node.left);
} }
if (poll.right != null) { if (node.right != null) {
deque.offer(poll.right); deque.offer(node.right);
} }
} }
} }