Home > Back-end >  Find a BUG - a sequence of binary tree traversal
Find a BUG - a sequence of binary tree traversal

Time:10-26

A binary tree for you, please return it in accordance with the sequence traversal node values, (namely step by step, from left to right to access all the nodes),
Example: binary tree: [3,9,20, null, null, 15, 7],
3
/\
September 20
/\
15 July
Returns to its level traversal results:

[3],
[9],
[15, 7]
]
Now my code output below, what's the problem????????????????
Input,9,20,1,2,3,15 [3]
My code output [[3], [20], [15]]
The expected output should be the result of [[3], [9], [1,2,3,15]]

/* ** Definition for a binary tree node. 
* struct TreeNode {
* int val.
* TreeNode * left;
* TreeNode * right;
* TreeNode (int x) : val (x), left (NULL), right (NULL) {}
*};
*/
The class Solution {
Public:
Vector LevelOrder (root TreeNode *) {
Vector Re;
If (root==NULL)
Return re;
DequeInt size=1;
While (size)
{
Vector Hang;
TreeNode * temp=the queue in front ();
Hang the push_back (temp - & gt; Val);
If (temp - & gt; Left!=NULL)
The queue. The push_back (temp - & gt; Left);
If (temp - & gt; Right!=NULL)
The queue. The push_back (temp - & gt; Right);
Queue. Pop_front ();
Size -;
If (size==0)
{
Size=queue. The size ();
Re. The push_back (hang);
}
}
Return re;
}
};
  • Related