File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -2451,6 +2451,30 @@ func maxDepth(_ root: TreeNode?) -> Int {
2451
2451
}
2452
2452
```
2453
2453
2454
+ Scala :
2455
+ ```scala
2456
+ // 104.二叉树的最大深度
2457
+ object Solution {
2458
+ import scala .collection .mutable
2459
+ def maxDepth (root : TreeNode ): Int = {
2460
+ if (root == null ) return 0
2461
+ val queue = mutable.Queue [TreeNode ]()
2462
+ queue.enqueue(root)
2463
+ var depth = 0
2464
+ while (! queue.isEmpty) {
2465
+ val len = queue.length
2466
+ depth += 1
2467
+ for (i <- 0 until len) {
2468
+ val curNode = queue.dequeue()
2469
+ if (curNode.left != null ) queue.enqueue(curNode.left)
2470
+ if (curNode.right != null ) queue.enqueue(curNode.right)
2471
+ }
2472
+ }
2473
+ depth
2474
+ }
2475
+ }
2476
+ ```
2477
+
2454
2478
# 111 .二叉树的最小深度
2455
2479
2456
2480
[力扣题目链接](https:// leetcode- cn.com/ problems/ minimum- depth- of- binary- tree/ )
@@ -2670,6 +2694,30 @@ func minDepth(_ root: TreeNode?) -> Int {
2670
2694
}
2671
2695
```
2672
2696
2697
+ Scala :
2698
+ ```scala
2699
+ // 111.二叉树的最小深度
2700
+ object Solution {
2701
+ import scala .collection .mutable
2702
+ def minDepth (root : TreeNode ): Int = {
2703
+ if (root == null ) return 0
2704
+ var depth = 0
2705
+ val queue = mutable.Queue [TreeNode ]()
2706
+ queue.enqueue(root)
2707
+ while (! queue.isEmpty) {
2708
+ depth += 1
2709
+ val len = queue.size
2710
+ for (i <- 0 until len) {
2711
+ val curNode = queue.dequeue()
2712
+ if (curNode.left != null ) queue.enqueue(curNode.left)
2713
+ if (curNode.right != null ) queue.enqueue(curNode.right)
2714
+ if (curNode.left == null && curNode.right == null ) return depth
2715
+ }
2716
+ }
2717
+ depth
2718
+ }
2719
+ }
2720
+ ```
2673
2721
2674
2722
# 总结
2675
2723
You can’t perform that action at this time.
0 commit comments