二叉树级顺序遍历调试

时间:2019-06-14 12:13:50

标签: 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;
 *     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列表的内容。 请帮助我修复错误。

我相信list.clear()也会清除res中添加的列表项。

就这样

x=34;
list.add(x);
x=45;
System.out.println(list); // it will still print [34]

,但使用列表列表,并向其中添加项目后,如果您修改内部列表,它也会修改列表列表。 为什么?

int x=3;
li.add(x);
x=45;
res.add(li);
System.out.println(li);
li.remove(0);
li.add(23);
System.out.println(res);

2 个答案:

答案 0 :(得分:1)

之所以会发生这种情况,是因为您正在操纵一个对象,但在原始类型上却不会发生这种情况


您使用的是一个list实例,将列表添加到外部列表中后,您仍在操纵相同的实例,因此总是引用相同 { {1}}实例

您要在外部列表中多次添加它并清除它,您需要在每次迭代中创建新实例:

List

答案 1 :(得分:0)

我在C ++中使用级别顺序遍历的工作代码

输出:

[
  [ 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);
}