Two steps of binary tree traversal


Binary tree hierarchy traversal, use the queue this data structure.

1. Binary tree level traversal (1)

The idea is: first put the follower node into the queue, when the queue is not empty, take out a node from the queue each time, and access its left and right subtrees into the queue at the same time. (Of course, it enters the queue only when its subtree is not empty)

Title description

Given a binary tree, return the value of its nodes traversed by level. (That is, visit all nodes layer by layer, from left to right).

For example:
Given a binary tree: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
returns the result of its level traversal:

[
[3],
[9,20],
[15,7]
]

Ideas

First put the follower node into the queue, when the queue is not empty, take out a node from the queue each time, visit it and put its left and right subtrees into the queue at the same time. (Of course, it enters the queue only when its subtree is not empty)

Note : The use of the for loop in the middle is to organize the results of each layer into a list, so every time all nodes in the current queue are traversed, that is, all nodes in the current layer, so that they can be put To a list, satisfy the result form in the question.

Java code

/**
 * 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) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        if(root == null) return result;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
            int size = q.size();
            temp = new ArrayList<>();
            for(int i = 0;i<size;i++){
                TreeNode node = q.poll();
                temp.add(node.val);
                if(node.left!=null)
                    q.offer(node.left);
                if(node.right!=null)
                    q.offer(node.right);
            }
            result.add(temp);
        }
        return result;
    }
}

As a comparison: the example of level traversal in traditional textbooks only requires printing out the results of level traversal, and does not require the output of the two-dimensional list in the title. That would be simpler, and the middle for loop is not needed, because it does not require the nodes of a layer to be organized into a list.

Java code (printing hierarchy traversal)

public void levelOrder(TreeNode root) {
        if(root == null) return result;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
                TreeNode node = q.poll();
                //打印节点
                System.out.println(node.val);
 				//子节点入队列
                if(node.left!=null)
                    q.offer(node.left);
                if(node.right!=null)
                    q.offer(node.right);
        }
    }

2. Binary tree level traversal (two)

Title description

Given a binary tree, return its node value to traverse from the bottom to the top. (That is, from the layer where the leaf node is located to the layer where the root node is located, traversing from left to right layer by layer)

For example:
Given a binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
returns its bottom-up level traversal as:

[
[15,7],
[9,20],
[3]
]

Ideas

Look, isn't this just the result of the first question "Level Traversal"! The previous question is to traverse from top to bottom, and this question is to traverse from bottom to top. So you can add the sentence Collections.reverse(result) at the end of the code in the above question.

In fact, where is it necessary to get the result and then flip it. When the result list is generated, every insertion from the beginning is the opposite result. Don't forget that List has a list.add(0,item) function, you can choose to add it to any position of the List.

Java code

/**
 * 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>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if(root == null) return result;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
            int size = q.size();
            List<Integer> tmp = new ArrayList<>();
            for(int i = 0;i<size;i++){
                TreeNode node = q.poll();
                tmp.add(node.val);
                if(node.left!=null) q.offer(node.left);
                if(node.right!=null) q.offer(node.right);
            }
            result.add(0,tmp);//每次添加到头部
        }
        return result;
    }
}

Guess you like

Origin blog.csdn.net/fxjzzyo/article/details/88270675