@@ -29,48 +29,41 @@ struct list_head {
2929 struct list_head * next , * prev ;
3030};
3131
32- static inline void
33- INIT_LIST_HEAD (struct list_head * list )
32+ static inline void INIT_LIST_HEAD (struct list_head * list )
3433{
3534 list -> next = list -> prev = list ;
3635}
3736
38- static inline int
39- list_empty (const struct list_head * head )
37+ static inline int list_empty (const struct list_head * head )
4038{
4139 return (head -> next == head );
4240}
4341
44- static inline void
45- __list_add (struct list_head * new , struct list_head * prev , struct list_head * next )
42+ static inline void __list_add (struct list_head * new , struct list_head * prev , struct list_head * next )
4643{
4744 next -> prev = new ;
4845 new -> next = next ;
4946 new -> prev = prev ;
5047 prev -> next = new ;
5148}
5249
53- static inline void
54- list_add (struct list_head * _new , struct list_head * head )
50+ static inline void list_add (struct list_head * _new , struct list_head * head )
5551{
5652 __list_add (_new , head , head -> next );
5753}
5854
59- static inline void
60- list_add_tail (struct list_head * _new , struct list_head * head )
55+ static inline void list_add_tail (struct list_head * _new , struct list_head * head )
6156{
6257 __list_add (_new , head -> prev , head );
6358}
6459
65- static inline void
66- __list_del (struct list_head * entry )
60+ static inline void __list_del (struct list_head * entry )
6761{
6862 entry -> next -> prev = entry -> prev ;
6963 entry -> prev -> next = entry -> next ;
7064}
7165
72- static inline void
73- list_del (struct list_head * entry )
66+ static inline void list_del (struct list_head * entry )
7467{
7568 __list_del (entry );
7669 entry -> next = entry -> prev = NULL ;
@@ -135,41 +128,30 @@ static int** levelOrder(struct TreeNode* root, int** columnSizes, int* returnSiz
135128 }
136129
137130 struct list_head free_list ;
138- struct list_head bfs_queue0 ;
139- struct list_head bfs_queue1 ;
131+ struct list_head q0 ;
132+ struct list_head q1 ;
140133 INIT_LIST_HEAD (& free_list );
141- INIT_LIST_HEAD (& bfs_queue0 );
142- INIT_LIST_HEAD (& bfs_queue1 );
134+ INIT_LIST_HEAD (& q0 );
135+ INIT_LIST_HEAD (& q1 );
143136
144137 int * * results = malloc (BST_MAX_LEVEL * sizeof (int * ));
145138 * columnSizes = malloc (BST_MAX_LEVEL * sizeof (int ));
146139 memset (* columnSizes , 0 , BST_MAX_LEVEL * sizeof (int ));
147140
148141 int level = 0 ;
149- struct bfs_node * new ;
150- if (root -> left != NULL ) {
151- new = node_new (& free_list , root -> left );
152- list_add_tail (& new -> link , & bfs_queue0 );
153- }
154-
155- if (root -> right != NULL ) {
156- new = node_new (& free_list , root -> right );
157- list_add_tail (& new -> link , & bfs_queue0 );
158- }
159-
160- results [level ] = malloc (sizeof (int ));
161- results [level ][0 ] = root -> val ;
162- (* columnSizes )[level ] = 1 ;
142+ struct bfs_node * new = node_new (& free_list , root );
143+ list_add_tail (& new -> link , & q0 );
163144
164- while (!list_empty (& bfs_queue0 ) || !list_empty (& bfs_queue1 )) {
165- if (++ level & 1 ) {
166- queue (& bfs_queue0 , & bfs_queue1 , & free_list , results , * columnSizes , level );
145+ while (!list_empty (& q0 ) || !list_empty (& q1 )) {
146+ if (level & 0x1 ) {
147+ queue (& q1 , & q0 , & free_list , results , * columnSizes , level );
167148 } else {
168- queue (& bfs_queue1 , & bfs_queue0 , & free_list , results , * columnSizes , level );
149+ queue (& q0 , & q1 , & free_list , results , * columnSizes , level );
169150 }
151+ level ++ ;
170152 }
171153
172- * returnSize = level + 1 ;
154+ * returnSize = level ;
173155 return results ;
174156}
175157
0 commit comments