2
头图

Sequence Traversal of Binary Tree II

Title description: Given a binary tree, return the bottom-up traversal of its node value. (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)

Please refer to LeetCode official website for example description.

Source: LeetCode
Link: https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Solution 1: Sequence traversal
  • First, if the root node is empty, return an empty result set directly.
  • If the root node is not empty, traverse the nodes of each layer through the queue. The specific processing process is as follows:

    • First put the root node into the queue;
    • Traverse the current number of nodes in the queue, which is the result of the current layer, and then put the left and right non-empty child nodes of the current layer node into the queue;
    • Then continue to traverse the nodes of the next layer in the queue until the queue is empty.
  • The result obtained in this way is the result of the layer sequence traversal from top to bottom. Finally, the method Collections.reverse(result); called to arrange the result set in reverse order to get the bottom-up layer sequence traversal.
import com.kaesar.leetcode.TreeNode;

import java.util.*;

public class LeetCode_107 {
    /**
     * 层序遍历
     *
     * @param root
     * @return
     */
    public static List<List<Integer>> levelOrderBottom(TreeNode root) {
        // 初始化结果集
        List<List<Integer>> result = new ArrayList<>();
        // 如果根节点为空,直接返回result
        if (root == null) {
            return result;
        }
        // 用一个队列存每一层的节点
        Queue<TreeNode> nodes = new LinkedList<>();
        nodes.add(root);
        // 遍历每一层的节点,将节点的值放入list中
        while (!nodes.isEmpty()) {
            int count = nodes.size();
            List<Integer> curLevel = new ArrayList<>();
            while (count > 0) {
                TreeNode cur = nodes.poll();
                curLevel.add(cur.val);
                if (cur.left != null) {
                    nodes.add(cur.left);
                }
                if (cur.right != null) {
                    nodes.add(cur.right);
                }
                count--;
            }
            result.add(curLevel);
        }
        // 最后将得到的结果集逆序,得到从最下层 -> 最上层的遍历结果
        Collections.reverse(result);
        return result;
    }

    public static void main(String[] args) {
        // 测试用例
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);

        for (List<Integer> integers : levelOrderBottom(root)) {
            for (Integer integer : integers) {
                System.out.print(integer + " ");
            }
            System.out.println();
        }
    }
}
[Daily Message] How many things are, always in a hurry, the world is changing, time is too short, ten thousand years are too long, and the day is too long.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!