3
头图

Right view of binary tree

Topic description: Given the root node root of a binary tree, imagine yourself standing on the right side of it, in order from top to bottom, return the node values that can be seen from the right side.

For example descriptions, please refer to the official website of LeetCode.

Source: LeetCode
Link: https://leetcode-cn.com/problems/binary-tree-right-side-view/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Solution 1: Level order traversal of binary tree

The binary tree is traversed in hierarchical order using the characteristics of the first-in, first-out queue. The specific processing process is as follows:

  • Initialize a queue and add the root node to the queue as the first layer;
  • Loop through the elements of each layer in the queue, temporarily store them in a two-dimensional List, and add the nodes of the next layer to the queue in the order from left to right;
  • The traversal ends until the queue is empty.

Finally, take the last element of each layer from the two-dimensional List in order, and return.

 import com.kaesar.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class LeetCode_199 {
    /**
     * 层序遍历
     * 通过使用队列先进先出的方式遍历二叉树
     *
     * @param root
     * @return
     */
    public static List<Integer> rightSideView(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        // 暂存每一层的节点
        List<List<Integer>> numsList = new ArrayList<>();
        Queue<TreeNode> nodes = new LinkedList<>();
        nodes.add(root);
        // 遍历每一层的节点,并将下一层的节点按从左到右的顺序加入到队列中
        while (!nodes.isEmpty()) {
            List<Integer> nums = new ArrayList<>();
            int count = nodes.size();
            for (int i = 0; i < count; i++) {
                TreeNode cur = nodes.poll();
                nums.add(cur.val);
                if (cur.left != null) {
                    nodes.add(cur.left);
                }
                if (cur.right != null) {
                    nodes.add(cur.right);
                }
            }
            numsList.add(nums);
        }

        List<Integer> result = new ArrayList<>();
        // 按顺序取每一层的最后一个元素
        for (List<Integer> integers : numsList) {
            result.add(integers.get(integers.size() - 1));
        }
        return result;
    }

    public static void main(String[] args) {
        /**
         * 测试用例:
         *     1
         *   2   3
         *    5   4
         */
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.right = new TreeNode(5);
        root.right.right = new TreeNode(4);

        // 期望输出: 1,3,4
        for (Integer integer : rightSideView(root)) {
            System.out.print(integer + " ");
        }
    }
}
[Daily Message] If a person strives to seek improvement, do his job well, find happiness for himself, and is competitive, then I will be good to win!

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

玉树临风,仙姿佚貌!