1, Hierarchical traversal of binary tree
It is unrealistic to traverse directly, because there are more nodes in the future, and not all nodes in each layer are 2k-1.
If you traverse a node, save the child of the node and get it from the structure next time. Here, the child is saved in a queue (the node first placed in the space is accessed first).
Steps:
1. First, you need to create a new queue - > the elements placed in the queue in the future are references to nodes
2. Put the root into the queue first
3. If the queue is not empty, the loop performs the following operations:
Get queue header element - poll
Traverse the node
Detect if there is a left child in the node, and let the left child enter the queue
If the node has a right child, let the right child enter the queue
Code implementation:
public void levelOrder(){ if(root==null){ return; } //The binary tree is not empty, and the sequence traversal of the binary tree needs to be completed with the help of queue Queue<BTNode> q=new LinkedList<>(); q.offer(root); while(!q.isEmpty()){ BTNode cur=q.poll(); //Get the queue header element and get out of the queue System.out.print(cur.value+" "); //If the left child of cur exists, let the left child of cur enter the queue if(cur.left!=null){ q.offer(cur.left); } //If the right child of cur exists, let the right child of cur enter the queue if(cur.right!=null){ q.offer(cur.right); } } }
2, Sequence traversal of binary tree (LeetCode)
1. Title Description
To give you a binary tree, please return the node values obtained by traversing in sequence. (that is, access all nodes from left to right layer by layer).
2. Input and output examples
Example:
Binary tree: [3,9,20,null,null,15,7],
Return its sequence traversal result:
[
[3],
[9,20],
[15,7]
]
3. Train of thought analysis
The return value required by the topic is a two-dimensional array. One layer of nodes are placed in a one-dimensional array, which means that we save one layer of nodes at a time
If we save one layer at a time, we have to traverse all the elements in the queue every time. However, the number of elements in the queue will be different with the number of layers. Use levelSize to record the number of elements
The others are the same as those implemented above
4. Code implementation
class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> tree=new ArrayList<>(); if(root==null){ return tree; } Queue<TreeNode> q=new LinkedList<>(); q.offer(root); while(!q.isEmpty()){ //The nodes of the same layer are saved in the queue //Traverse the nodes of this layer at one time int levelSize=q.size(); List<Integer> level=new ArrayList<>(); for(int i=0;i<levelSize;i++){ TreeNode cur=q.poll(); level.add(cur.val); if(cur.left!=null){ q.offer(cur.left); } if(cur.right!=null){ q.offer(cur.right); } } //The node of the upper layer has been traversed, and the next layer node is just saved in the queue tree.add(level); } return tree; } }
3, Sequence traversal II (LeetCode)
1. Title Description
Given a binary tree, return the bottom-up sequence traversal of its node value. (that is, traverse from left to right layer by layer from the layer where the leaf node is located to the layer where the root node is located)
2. Input and output examples
Given binary tree [3,9,20,null,null,15,7],
Return its bottom-up sequence traversal as:
[
[15,7],
[9,20],
[3]
]
3. Train of thought analysis
The change made in the above question is only output in reverse order
It is not difficult for us to think that the stack is first in and then out, so here we can temporarily store the elements originally to be directly stored in the tree in the stack, and then pour them from the stack to the tree after all nodes are stored, so as to realize inversion
4. Code implementation
class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> tree=new ArrayList<>(); if(root==null){ return tree; } Queue<TreeNode> q=new LinkedList<>(); Stack<List<Integer>> s=new Stack<>(); q.offer(root); while(!q.isEmpty()){ //The nodes of the same layer are saved in the queue //Traverse the nodes of this layer at one time int levelSize=q.size(); List<Integer> level=new ArrayList<>(); for(int i=0;i<levelSize;i++){ TreeNode cur=q.poll(); level.add(cur.val); if(cur.left!=null){ q.offer(cur.left); } if(cur.right!=null){ q.offer(cur.right); } } //The node of the upper layer has been traversed, and the next layer node is just saved in the queue //Temporarily store the elements of each layer on the stack s.push(level); } //Importing tree from stack to realize inversion while(!s.empty()){ tree.add(s.pop()); } return tree; } }