Java 二叉树级顺序遍历调试
我有一棵二叉树Java 二叉树级顺序遍历调试,java,data-structures,binary-tree,Java,Data Structures,Binary Tree,我有一棵二叉树 3 / \ 9 20 / \ 15 7 我想以这种格式打印它的级别顺序遍历 [ [3], [9,20], [15,7] ] 因此,我使用一个队列和两个列表编写了这段代码 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right;
3
/ \
9 20
/ \
15 7
我想以这种格式打印它的级别顺序遍历
[
[3],
[9,20],
[15,7]
]
因此,我使用一个队列和两个列表编写了这段代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> queue=new LinkedList<TreeNode>();
List<Integer> list=new ArrayList<>();
List<List<Integer>> res=new LinkedList<>();
if(root!=null)
{
queue.add(root);
}
while(!queue.isEmpty())
{
int size=queue.size();
for(int i=0;i<size;i++)
{
TreeNode tempNode=queue.poll();
list.add(tempNode.val);
if(tempNode.left!=null)
queue.add(tempNode.left);
if(tempNode.right!=null)
queue.add(tempNode.right);
}
res.add(list);
list.clear();
}
return res;
}
}
我花了1个多小时调试这个问题,我确信我的代码是正确的(事实并非如此!)
我不知道在我将数据添加到res列表后,是什么在清除res列表。
请帮我纠正这个错误
我相信list.clear()还会清除res中添加的列表项
我想这是真的
x=34;
list.add(x);
x=45;
System.out.println(list); // it will still print [34]
但使用列表的列表,并在向其添加项目后,如果您修改内部列表。。它还会修改您的列表列表。
为什么?
发生这种情况是因为您在操作对象,而不是在基本类型上
您使用的是单个
列表
实例,在将列表添加到外部列表后,您仍然在操作同一个实例,您总是引用相同的列表实例
如果要在外部列表中多次添加并清除它,则需要在每次迭代中创建新实例:
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0 ; i<size;i++){
TreeNode tempNode = queue.poll();
list.add(tempNode.val);
if(tempNode.left!=null)
queue.add(tempNode.left);
if(tempNode.right!=null)
queue.add(tempNode.right);
}
res.add(list);
list = new ArrayList<>();
}
while(!queue.isEmpty()){
int size=queue.size();
对于(int i=0;i C++中的工作代码,使用级别顺序遍历< /p>
输出:
[
[ 10 ],
[ 11 9 ],
[ 7 12 15 8 ],
[ 13 14 16 18 ],
]
#include <bits/stdc++.h>
using namespace std;
struct node{
int key;
struct node *left;
struct node *right;
};
struct node *newnode(int key){
struct node *Node= new node;
Node->key=key;
Node->left=NULL;
Node->right=NULL;
return Node;
}
void printNestedList(list<list<int> > nested_list)
{
cout << "[\n";
list<list<int> >::iterator nested_list_itr;
for (nested_list_itr = nested_list.begin();
nested_list_itr != nested_list.end();
++nested_list_itr) {
cout << " [";
list<int>::iterator single_list_itr;
list<int>& single_list_pointer = *nested_list_itr;
for (single_list_itr = single_list_pointer.begin();
single_list_itr != single_list_pointer.end();
single_list_itr++) {
cout << " " << *single_list_itr << " ";
}
cout << "],\n";
}
cout << "]";
}
void levelorder_traversal(struct node *temp){
int l=1,level=1;
pair <struct node *, int> p;
queue<pair <struct node *, int> > q;
q.push(make_pair(temp, l));
list<list<int> > nested_list;
list<int> single_list;
single_list.push_back(temp->key);
while(!q.empty()){
struct node *temp= q.front().first;
l= q.front().second;
q.pop();
if(temp->left){
p = make_pair(temp->left, (l+1));
q.push(p);
if(l+1>level){
nested_list.push_back(single_list);
single_list.erase(single_list.begin(),single_list.end());
level++;
}
single_list.push_back(temp->left->key);
}
if(temp->right){
p = make_pair(temp->right, (l+1));
q.push(p);
if(l+1>level){
nested_list.push_back(single_list);
single_list.erase(single_list.begin(),single_list.end());
level++;
}
single_list.push_back(temp->right->key);
}
if(q.empty()){
nested_list.push_back(single_list);
}
}
cout<<endl;
printNestedList(nested_list);
}
int main(){
struct node* root = newnode(10);
root->left = newnode(11);
root->left->left = newnode(7);
root->left->right = newnode(12);
root->right = newnode(9);
root->right->left = newnode(15);
root->right->right = newnode(8);
root->left->left->left = newnode(13);
root->left->left->right = newnode(14);
root->left->right->left = newnode(16);
root->left->right->right = newnode(18);
levelorder_traversal(root);
}
代码:
[
[ 10 ],
[ 11 9 ],
[ 7 12 15 8 ],
[ 13 14 16 18 ],
]
#include <bits/stdc++.h>
using namespace std;
struct node{
int key;
struct node *left;
struct node *right;
};
struct node *newnode(int key){
struct node *Node= new node;
Node->key=key;
Node->left=NULL;
Node->right=NULL;
return Node;
}
void printNestedList(list<list<int> > nested_list)
{
cout << "[\n";
list<list<int> >::iterator nested_list_itr;
for (nested_list_itr = nested_list.begin();
nested_list_itr != nested_list.end();
++nested_list_itr) {
cout << " [";
list<int>::iterator single_list_itr;
list<int>& single_list_pointer = *nested_list_itr;
for (single_list_itr = single_list_pointer.begin();
single_list_itr != single_list_pointer.end();
single_list_itr++) {
cout << " " << *single_list_itr << " ";
}
cout << "],\n";
}
cout << "]";
}
void levelorder_traversal(struct node *temp){
int l=1,level=1;
pair <struct node *, int> p;
queue<pair <struct node *, int> > q;
q.push(make_pair(temp, l));
list<list<int> > nested_list;
list<int> single_list;
single_list.push_back(temp->key);
while(!q.empty()){
struct node *temp= q.front().first;
l= q.front().second;
q.pop();
if(temp->left){
p = make_pair(temp->left, (l+1));
q.push(p);
if(l+1>level){
nested_list.push_back(single_list);
single_list.erase(single_list.begin(),single_list.end());
level++;
}
single_list.push_back(temp->left->key);
}
if(temp->right){
p = make_pair(temp->right, (l+1));
q.push(p);
if(l+1>level){
nested_list.push_back(single_list);
single_list.erase(single_list.begin(),single_list.end());
level++;
}
single_list.push_back(temp->right->key);
}
if(q.empty()){
nested_list.push_back(single_list);
}
}
cout<<endl;
printNestedList(nested_list);
}
int main(){
struct node* root = newnode(10);
root->left = newnode(11);
root->left->left = newnode(7);
root->left->right = newnode(12);
root->right = newnode(9);
root->right->left = newnode(15);
root->right->right = newnode(8);
root->left->left->left = newnode(13);
root->left->left->right = newnode(14);
root->left->right->left = newnode(16);
root->left->right->right = newnode(18);
levelorder_traversal(root);
}
#包括
使用名称空间std;
结构节点{
int键;
结构节点*左;
结构节点*右;
};
结构节点*newnode(int键){
结构节点*节点=新节点;
节点->键=键;
节点->左=空;
节点->右=空;
返回节点;
}
作废打印嵌套列表(列表嵌套列表)
{
(右){
p=配对(温度->右侧,(l+1));
q、 推(p);
如果(l+1>水平){
嵌套列表。向后推(单个列表);
single_list.erase(single_list.begin(),single_list.end());
级别++;
}
单键列表。向后推(临时->右->键);
}
if(q.empty()){
嵌套列表。向后推(单个列表);
}
}
coutleft->left=newnode(7);
根->左->右=新节点(12);
根->右=新节点(9);
根->右->左=新节点(15);
根->右->右=新节点(8);
根->左->左->左=新节点(13);
根->左->左->右=新节点(14);
根->左->右->左=新节点(16);
根->左->右->右=新节点(18);
levelorder_遍历(根);
}
首先替换list.clear()代码>带有列表=新的ArrayList()代码>相关:可能是@mridulmital的副本,它发生在对象上,而不是原语上types@MridulMittal基元类型没有可以调用的clear()之类的方法。所以这真的是不可比的。