LeetCode 199. Binary Tree Right Side View Posted on 2021-07-21 In Leetcode , Tree Symbols count in article: 720 Reading time ≈ 1 mins. QuestionGiven the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. https://leetcode.com/problems/binary-tree-right-side-view Solution Solution1 – BFS 123456789101112131415161718class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> res = new ArrayList<>(); if(root == null) return res; Queue<TreeNode> q = new LinkedList<>(); q.offer(root); while(!q.isEmpty()){ int size = q.size(); res.add(q.peek().val); for(int i = 0; i < size; i++){ TreeNode cur = q.poll(); if(cur.right != null) q.offer(cur.right); if(cur.left != null) q.offer(cur.left); } } return res; }} Complexity: Time complexity: O(n) Space complexity: O(n) Post author: MrainW Post link: https://mrainw.github.io/2021/07/21/LeetCode 199. Binary Tree Right Side View/ Copyright Notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally. Welcome to my other publishing channels WeChat