Skip to content

Commit

Permalink
Leetcode 104 Maximum Depth of Binary Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajionline committed Jun 26, 2023
1 parent 3e7950b commit 62158bc
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 0104_Maximum Depth of Binary Tree/MaximumDepthofBinaryTree4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int ans = 1;
Queue<Pair<TreeNode,Integer>> q = new LinkedList<>();
q.offer(new Pair<TreeNode,Integer>(root, 1));
while(q.size() > 0) {
Pair<TreeNode, Integer> pair = q.poll();
TreeNode node = pair.getKey();
int depth = pair.getValue();
ans = Math.max(ans, depth);
if(node.left != null)
q.offer(new Pair<TreeNode, Integer>(node.left, depth+1));

if(node.right != null)
q.offer(new Pair<TreeNode, Integer>(node.right, depth+1));
}

return ans;
}
}

0 comments on commit 62158bc

Please sign in to comment.