@@ -738,26 +738,36 @@ function maxDepth(root: TreeNode | null): number {
738
738
739
739
``` typescript
740
740
// 后续遍历(自下而上)
741
- function maxDepth(root : TreeNode | null ): number {
742
- if (root === null ) return 0 ;
743
- return Math .max (maxDepth (root .left ), maxDepth (root .right )) + 1 ;
744
- };
741
+ function maxDepth(root : Node | null ): number {
742
+ if (root === null ) return 0
743
+ let depth = 0
744
+ for (let i = 0 ; i < root .children .length ; i ++ ) {
745
+ depth = Math .max (depth , maxDepth (root .children [i ]))
746
+ }
747
+ return depth + 1
748
+ }
745
749
746
750
// 前序遍历(自上而下)
747
- function maxDepth(root : TreeNode | null ): number {
748
- function recur(node : TreeNode | null , count : number ) {
749
- if (node === null ) {
750
- resMax = resMax > count ? resMax : count ;
751
- return ;
751
+ function maxDepth(root : Node | null ): number {
752
+ if (root === null ) return 0
753
+
754
+ let depth: number = 0
755
+ const queue: Array <Node | null > = []
756
+ queue .push (root )
757
+
758
+ while (queue .length > 0 ) {
759
+ let len = queue .length
760
+ depth ++
761
+ for (let i = 0 ; i < len ; i ++ ) {
762
+ // 当前层遍历
763
+ let curNode: Node | null = queue .shift ()!
764
+ for (let j = 0 ; j < curNode .children .length ; j ++ ) {
765
+ if (curNode .children [j ]) queue .push (curNode .children [j ])
766
+ }
752
767
}
753
- recur (node .left , count + 1 );
754
- recur (node .right , count + 1 );
755
768
}
756
- let resMax: number = 0 ;
757
- let count: number = 0 ;
758
- recur (root , count );
759
- return resMax ;
760
- };
769
+ return depth
770
+ }
761
771
762
772
763
773
```
@@ -1022,6 +1032,61 @@ impl Solution {
1022
1032
max_depth
1023
1033
}
1024
1034
```
1035
+ ### C#
1036
+ ``` C#
1037
+ // 递归法
1038
+ public int MaxDepth (TreeNode root ) {
1039
+ if (root == null ) return 0 ;
1040
+
1041
+ int leftDepth = MaxDepth (root .left );
1042
+ int rightDepth = MaxDepth (root .right );
1043
+
1044
+ return 1 + Math .Max (leftDepth , rightDepth );
1045
+ }
1046
+ ```
1047
+ ``` C#
1048
+ // 前序遍历
1049
+ int result = 0 ;
1050
+ public int MaxDepth (TreeNode root )
1051
+ {
1052
+ if (root == null ) return result ;
1053
+ GetDepth (root , 1 );
1054
+ return result ;
1055
+ }
1056
+ public void GetDepth (TreeNode root , int depth )
1057
+ {
1058
+ result = depth > result ? depth : result ;
1059
+ if (root .left == null && root .right == null ) return ;
1060
+
1061
+ if (root .left != null )
1062
+ GetDepth (root .left , depth + 1 );
1063
+ if (root .right != null )
1064
+ GetDepth (root .right , depth + 1 );
1065
+ return ;
1066
+ }
1067
+ ```
1068
+ ``` C#
1069
+ // 迭代法
1070
+ public int MaxDepth (TreeNode root )
1071
+ {
1072
+ int depth = 0 ;
1073
+ Queue < TreeNode > que = new ();
1074
+ if (root == null ) return depth ;
1075
+ que .Enqueue (root );
1076
+ while (que .Count != 0 )
1077
+ {
1078
+ int size = que .Count ;
1079
+ depth ++ ;
1080
+ for (int i = 0 ; i < size ; i ++ )
1081
+ {
1082
+ var node = que .Dequeue ();
1083
+ if (node .left != null ) que .Enqueue (node .left );
1084
+ if (node .right != null ) que .Enqueue (node .right );
1085
+ }
1086
+ }
1087
+ return depth ;
1088
+ }
1089
+ ```
1025
1090
1026
1091
<p align =" center " >
1027
1092
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
0 commit comments