Topic:

Given an n-tree, returns the value of its nodes

Sequential traversal (that is, layer by layer from left to right).

The serialized input to the tree is traversed in order, with each set of child nodes separated by null values (see example).

// Definition for a Node.class Node {public: int val; vector<Node*> children; Node() {} Node(int _val) { val = _val; } Node(int _val, vector<Node*> _children) { val = _val; children = _children; }}; class Solution {public: vector<vector<int>> levelOrder(Node* root) { }};Copy the code

Answer key:

List<Integer> values = new ArrayList<>();
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
    Node nextNode = queue.remove();
    values.add(nextNode.val);
    for (Node child : nextNode.children) {
        queue.add(child);
    }
}
Copy the code

The rows should be arranged from top to bottom. Because we traverse the tree starting at the root and then go down to the node closest to the root, this is breadth-first search. We use queues for breadth-first search, and queues are first-in, first-out. The wrong choice here is to use a stack, which should be used for depth-first searches. Let’s use a queue-based traversal algorithm on a tree to see what it does. This is a basic algorithm that you should keep in mind.

Queue: A first-in, first-out queue for sorting nodes

Values: The record used for sequential traversal. The value is integer and the value is stored

Vector does:

Node is a class with its own bai meaning.

Du represented by vector is a container that can hold any custom classes. Dao and array are similar to Zhuan but different.

Vector ANS means an object that defines a container called ANS that holds Node entities.

The usual functions of vector are:

Node tmp;

ans.push_back(tmp); // Put an entity behind it

for(int i=0; i<ans.size(); I++) {// enumerate entities

tmp=ans[i];

}

tmp = ans.back(); // TMP = ans[ans.size()-1];

ans.pop_back(); // Pop an entity from behind

Queue operations:

Remove () and poll() are both used to remove an element from the Queue head. In the case of an empty queue element, the remove() method throws NoSuchElementException and the poll() method only returns NULL.

For (element type t element variable x: traversal object obj)

The colon for loop is called the foreach loop. The foreach statement is one of the new features of java5. Foreach provides developers with great convenience in traversing groups of numbers and collections. Foreach statements are special, simplified versions of for statements, but foreach statements do not completely replace for statements. However, any foreach statement can be rewritten as a for statement version. Foreach is not a keyword, and this particular for statement format is commonly referred to as a “foreach” statement. Foreach literally means “for every”. That’s actually what it means. Foreach: for(element type t element variable x: traversal object obj){Java statements that reference x; }

Sample code:

List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); for (String x : list) { System.out.println(x); } Object s[] = list.toArray(); for (Object x : s) { System.out.println(x.toString()); // Print the values of each array element}Copy the code