原题题目
代码实现(首刷自解)
class Solution {
public :
vector< vector< int >> levelOrder ( TreeNode* root) {
int count = 0 ;
vector< TreeNode* > temp, v;
vector< vector< int >> ret;
if ( root) temp. push_back ( root) ;
while ( ! temp. empty ( ) )
{
ret. push_back ( vector < int > ( ) ) ;
for ( const auto & root: temp)
{
if ( root-> left) v. push_back ( root-> left) ;
if ( root-> right) v. push_back ( root-> right) ;
ret[ count] . push_back ( root-> val) ;
}
temp = v;
v. clear ( ) ;
++ count;
}
return ret;
}
} ;
代码实现(二刷自解 C++ DAY 149)
class Solution {
public :
vector< vector< int >> levelOrder ( TreeNode* root) {
vector< TreeNode* > next, tmp;
vector< vector< int >> ret;
if ( ! root) return ret;
next. emplace_back ( root) ;
while ( next. size ( ) )
{
vector< TreeNode* > now = next;
next = tmp;
vector< int > temp;
for ( const auto & node: now)
{
temp. emplace_back ( node-> val) ;
if ( node-> left) next. emplace_back ( node-> left) ;
if ( node-> right) next. emplace_back ( node-> right) ;
}
ret. emplace_back ( temp) ;
}
return ret;
}
} ;
代码实现(三刷自解 C++ DAY 201 递归实现)
class Solution {
public :
int maxheight = - 1 ;
void depth_first_search ( TreeNode* root, vector< vector< int >> & visit, int height)
{
if ( ! root) return ;
if ( height > maxheight)
{
visit. emplace_back ( vector < int > ( ) ) ;
visit[ height] . emplace_back ( root-> val) ;
}
else visit[ height] . emplace_back ( root-> val) ;
maxheight = max ( maxheight, height) ;
depth_first_search ( root-> left, visit, height+ 1 ) ;
depth_first_search ( root-> right, visit, height+ 1 ) ;
}
vector< vector< int >> levelOrder ( TreeNode* root) {
vector< vector< int >> visit;
depth_first_search ( root, visit, 0 ) ;
return visit;
}
} ;
代码实现(四刷自解 DAY 269 层序遍历 C++)
class Solution {
public :
vector< vector< int >> levelOrder ( TreeNode* root) {
queue< TreeNode* > queue;
if ( root) {
queue. emplace ( root) ;
}
vector< vector< int >> ret;
vector< int > tmp;
while ( ! queue. empty ( ) ) {
int size = queue. size ( ) ;
tmp. clear ( ) ;
for ( int i = 0 ; i < size; ++ i)
{
auto ptr = queue. front ( ) ;
queue. pop ( ) ;
tmp. emplace_back ( ptr-> val) ;
if ( ptr-> left) { queue. emplace ( ptr-> left) ; }
if ( ptr-> right) { queue. emplace ( ptr-> right) ; }
}
ret. emplace_back ( tmp) ;
}
return ret;
}
} ;
代码实现(四刷自解 DAY 269 递归实现)
class Solution {
public :
void DepthFirstSearch ( vector< vector< int >> & ret, TreeNode* root, int level) {
if ( ! root) return ;
if ( ret. size ( ) < level + 1 ) {
ret. emplace_back ( vector < int > ( ) ) ;
}
ret[ level] . emplace_back ( root-> val) ;
DepthFirstSearch ( ret, root-> left, level+ 1 ) ;
DepthFirstSearch ( ret, root-> right, level+ 1 ) ;
return ;
}
vector< vector< int >> levelOrder ( TreeNode* root) {
vector< vector< int >> ret;
DepthFirstSearch ( ret, root, 0 ) ;
return ret;
}
} ;
代码实现(五刷自解 DAY 2 Golang)
func levelOrder ( root * TreeNode) [ ] [ ] int {
ret := make ( [ ] [ ] int , 0 )
if root == nil {
return ret
}
deq := make ( [ ] * TreeNode, 0 )
deq = append ( deq, root)
for len ( deq) != 0 {
size := len ( deq)
ret = append ( ret, [ ] int { } )
for i := 0 ; i < size; i++ {
ptr := deq[ i]
ret[ len ( ret) - 1 ] = append ( ret[ len ( ret) - 1 ] , ptr. Val)
left, right := ptr. Left, ptr. Right
if left != nil {
deq = append ( deq, left)
}
if right != nil {
deq = append ( deq, right)
}
}
deq = deq[ size: ]
}
return ret
}