File tree Expand file tree Collapse file tree 2 files changed +109
-0
lines changed Expand file tree Collapse file tree 2 files changed +109
-0
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Text ;
4
+
5
+ namespace leetcodeTest
6
+ {
7
+ public class UniqueBSTSln
8
+ {
9
+ // Definition for a binary tree node.
10
+ public class TreeNode
11
+ {
12
+ public int val ;
13
+ public TreeNode left ;
14
+ public TreeNode right ;
15
+ public TreeNode ( int x ) { val = x ; }
16
+ }
17
+ public IList < TreeNode > GenerateTrees ( int n )
18
+ {
19
+ if ( n == 0 )
20
+ return new List < TreeNode > ( ) ;
21
+ return binaryTrees ( 1 , n ) ;
22
+ }
23
+ private IList < TreeNode > binaryTrees ( int start , int end )
24
+ {
25
+ IList < TreeNode > rtn = new List < TreeNode > ( ) ;
26
+ if ( start > end )
27
+ {
28
+ rtn . Add ( null ) ; //care it
29
+ return rtn ;
30
+ }
31
+ for ( int i = start ; i <= end ; i ++ )
32
+ {
33
+ IList < TreeNode > lefts = binaryTrees ( start , i - 1 ) ;
34
+ IList < TreeNode > rights = binaryTrees ( i + 1 , end ) ;
35
+ foreach ( var leftNode in lefts )
36
+ {
37
+ foreach ( var rightNode in rights )
38
+ {
39
+ TreeNode root = new TreeNode ( i ) ;
40
+ root . left = leftNode ;
41
+ root . right = rightNode ;
42
+ rtn . Add ( root ) ;
43
+ }
44
+ }
45
+ }
46
+ return rtn ;
47
+ }
48
+
49
+
50
+ }
51
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Text ;
4
+
5
+ //103. Binary Tree Zigzag Level Order Traversal
6
+ namespace leetcodeTest
7
+ {
8
+ public class Solution {
9
+ private int lineno ; //从二层开始按行号编号
10
+
11
+ public IList < IList < int > > ZigzagLevelOrder ( TreeNode root ) {
12
+ if ( root == null )
13
+ return new List < IList < int > > ( ) ;
14
+ IList < IList < int > > rtn = new List < IList < int > > { new List < int > { root . val } } ; //第一层
15
+ var tmp = zigzagLevelOrder ( new List < TreeNode > { root } ) ; //tmp: 第二层~叶子层
16
+ if ( tmp != null && tmp . Count > 0 )
17
+ {
18
+ foreach ( var item in tmp )
19
+ rtn . Add ( item ) ;
20
+ }
21
+ return rtn ;
22
+ }
23
+ //第二层到叶子层: right to left; left to right; ...
24
+ private IList < IList < int > > zigzagLevelOrder ( IList < TreeNode > curLevel )
25
+ {
26
+ if ( curLevel == null || curLevel . Count == 0 )
27
+ return null ;
28
+ IList < IList < int > > rtn = new List < IList < int > > ( ) ;
29
+ List < TreeNode > nextn = new List < TreeNode > ( ) ;
30
+ foreach ( var item in curLevel )
31
+ {
32
+ if ( item . left != null )
33
+ nextn . Add ( item . left ) ;
34
+ if ( item . right != null )
35
+ nextn . Add ( item . right ) ;
36
+ }
37
+ if ( nextn . Count == 0 )
38
+ return null ;
39
+ if ( lineno % 2 == 0 ) //反转本层
40
+ {
41
+ List < int > tmp = nextn . Select ( r => r . val ) . ToList ( ) ;
42
+ tmp . Reverse ( ) ;
43
+ rtn . Add ( tmp ) ;
44
+ }
45
+ else
46
+ rtn . Add ( nextn . Select ( r=> r . val ) . ToList ( ) ) ; //正序
47
+
48
+ ++ lineno ;
49
+ var children = zigzagLevelOrder ( nextn ) ;
50
+ if ( children != null && children . Count > 0 )
51
+ {
52
+ foreach ( var item in children )
53
+ rtn . Add ( item ) ;
54
+ }
55
+ return rtn ;
56
+ }
57
+ }
58
+ }
You can’t perform that action at this time.
0 commit comments