Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ef9cefe

Browse files
committed
添加 0104.二叉树的最大深度.md Scala版本
1 parent e07a3ca commit ef9cefe

File tree

1 file changed

+93
-6
lines changed

1 file changed

+93
-6
lines changed

‎problems/0104.二叉树的最大深度.md‎

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ class solution:
468468

469469

470470
## go
471-
471+
### 104.二叉树的最大深度
472472
```go
473473
/**
474474
* definition for a binary tree node.
@@ -521,6 +521,8 @@ func maxdepth(root *treenode) int {
521521

522522
## javascript
523523

524+
### 104.二叉树的最大深度
525+
524526
```javascript
525527
var maxdepth = function(root) {
526528
if (root === null) return 0;
@@ -568,6 +570,8 @@ var maxDepth = function(root) {
568570
};
569571
```
570572

573+
### 559.n叉树的最大深度
574+
571575
N叉树的最大深度 递归写法
572576
```js
573577
var maxDepth = function(root) {
@@ -600,9 +604,9 @@ var maxDepth = function(root) {
600604
};
601605
```
602606

603-
## TypeScript:
607+
## TypeScript
604608

605-
> 二叉树的最大深度:
609+
### 104.二叉树的最大深度
606610

607611
```typescript
608612
// 后续遍历(自下而上)
@@ -645,7 +649,7 @@ function maxDepth(root: TreeNode | null): number {
645649
};
646650
```
647651

648-
> N叉树的最大深度
652+
### 559.n叉树的最大深度
649653

650654
```typescript
651655
// 后续遍历(自下而上)
@@ -675,6 +679,8 @@ function maxDepth(root: TreeNode | null): number {
675679

676680
## C
677681

682+
### 104.二叉树的最大深度
683+
678684
二叉树最大深度递归
679685
```c
680686
int maxDepth(struct TreeNode* root){
@@ -731,7 +737,8 @@ int maxDepth(struct TreeNode* root){
731737

732738
## Swift
733739

734-
>二叉树最大深度
740+
### 104.二叉树的最大深度
741+
735742
```swift
736743
// 递归 - 后序
737744
func maxDepth1(_ root: TreeNode?) -> Int {
@@ -770,7 +777,8 @@ func maxDepth(_ root: TreeNode?) -> Int {
770777
}
771778
```
772779

773-
>N叉树最大深度
780+
### 559.n叉树的最大深度
781+
774782
```swift
775783
// 递归
776784
func maxDepth(_ root: Node?) -> Int {
@@ -806,5 +814,84 @@ func maxDepth1(_ root: Node?) -> Int {
806814
}
807815
```
808816

817+
## Scala
818+
819+
### 104.二叉树的最大深度
820+
递归法:
821+
```scala
822+
object Solution {
823+
def maxDepth(root: TreeNode): Int = {
824+
def process(curNode: TreeNode): Int = {
825+
if (curNode == null) return 0
826+
// 递归左节点和右节点,返回最大的,最后+1
827+
math.max(process(curNode.left), process(curNode.right)) + 1
828+
}
829+
// 调用递归方法,return关键字可以省略
830+
process(root)
831+
}
832+
}
833+
```
834+
835+
迭代法:
836+
```scala
837+
object Solution {
838+
import scala.collection.mutable
839+
def maxDepth(root: TreeNode): Int = {
840+
var depth = 0
841+
if (root == null) return depth
842+
val queue = mutable.Queue[TreeNode]()
843+
queue.enqueue(root)
844+
while (!queue.isEmpty) {
845+
val len = queue.size
846+
for (i <- 0 until len) {
847+
val curNode = queue.dequeue()
848+
if (curNode.left != null) queue.enqueue(curNode.left)
849+
if (curNode.right != null) queue.enqueue(curNode.right)
850+
}
851+
depth += 1 // 只要有层次就+=1
852+
}
853+
depth
854+
}
855+
}
856+
```
857+
858+
### 559.n叉树的最大深度
859+
860+
递归法:
861+
```scala
862+
object Solution {
863+
def maxDepth(root: Node): Int = {
864+
if (root == null) return 0
865+
var depth = 0
866+
for (node <- root.children) {
867+
depth = math.max(depth, maxDepth(node))
868+
}
869+
depth + 1
870+
}
871+
}
872+
```
873+
874+
迭代法: (层序遍历)
875+
```scala
876+
object Solution {
877+
import scala.collection.mutable
878+
def maxDepth(root: Node): Int = {
879+
if (root == null) return 0
880+
var depth = 0
881+
val queue = mutable.Queue[Node]()
882+
queue.enqueue(root)
883+
while (!queue.isEmpty) {
884+
val len = queue.size
885+
depth += 1
886+
for (i <- 0 until len) {
887+
val curNode = queue.dequeue()
888+
for (node <- curNode.children) queue.enqueue(node)
889+
}
890+
}
891+
depth
892+
}
893+
}
894+
```
895+
809896
-----------------------
810897
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /