@@ -468,7 +468,7 @@ class solution:
468
468
469
469
470
470
## go
471
-
471
+ ### 104.二叉树的最大深度
472
472
``` go
473
473
/* *
474
474
* definition for a binary tree node.
@@ -521,6 +521,8 @@ func maxdepth(root *treenode) int {
521
521
522
522
## javascript
523
523
524
+ ### 104.二叉树的最大深度
525
+
524
526
``` javascript
525
527
var maxdepth = function (root ) {
526
528
if (root === null ) return 0 ;
@@ -568,6 +570,8 @@ var maxDepth = function(root) {
568
570
};
569
571
```
570
572
573
+ ### 559.n叉树的最大深度
574
+
571
575
N叉树的最大深度 递归写法
572
576
``` js
573
577
var maxDepth = function (root ) {
@@ -600,9 +604,9 @@ var maxDepth = function(root) {
600
604
};
601
605
```
602
606
603
- ## TypeScript:
607
+ ## TypeScript
604
608
605
- > 二叉树的最大深度:
609
+ ### 104. 二叉树的最大深度
606
610
607
611
``` typescript
608
612
// 后续遍历(自下而上)
@@ -645,7 +649,7 @@ function maxDepth(root: TreeNode | null): number {
645
649
};
646
650
```
647
651
648
- > N叉树的最大深度
652
+ ### 559.n叉树的最大深度
649
653
650
654
``` typescript
651
655
// 后续遍历(自下而上)
@@ -675,6 +679,8 @@ function maxDepth(root: TreeNode | null): number {
675
679
676
680
## C
677
681
682
+ ### 104.二叉树的最大深度
683
+
678
684
二叉树最大深度递归
679
685
``` c
680
686
int maxDepth (struct TreeNode* root){
@@ -731,7 +737,8 @@ int maxDepth(struct TreeNode* root){
731
737
732
738
## Swift
733
739
734
- > 二叉树最大深度
740
+ ### 104.二叉树的最大深度
741
+
735
742
``` swift
736
743
// 递归 - 后序
737
744
func maxDepth1 (_ root : TreeNode? ) -> Int {
@@ -770,7 +777,8 @@ func maxDepth(_ root: TreeNode?) -> Int {
770
777
}
771
778
```
772
779
773
- > N叉树最大深度
780
+ ### 559.n叉树的最大深度
781
+
774
782
``` swift
775
783
// 递归
776
784
func maxDepth (_ root : Node? ) -> Int {
@@ -806,5 +814,84 @@ func maxDepth1(_ root: Node?) -> Int {
806
814
}
807
815
```
808
816
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
+
809
896
-----------------------
810
897
<div align =" center " ><img src =https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width =500 > </img ></div >
0 commit comments