SHARE
TWEET

Untitled

a guest Sep 17th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Definition for a binary tree node.
  3.  * public class TreeNode {
  4.  *     int val;
  5.  *     TreeNode left;
  6.  *     TreeNode right;
  7.  *     TreeNode(int x) { val = x; }
  8.  * }
  9.  */
  10. class Solution {
  11.     public List<Integer> rightSideView(TreeNode root) {
  12.         List<Integer> res = new LinkedList<>();
  13.         if (root == null) return res;
  14.         Queue<TreeNode> q = new LinkedList<>();
  15.         q.offer(root);
  16.         while (!q.isEmpty()) {
  17.             TreeNode last = null;
  18.             int cnt = q.size();
  19.             for (int i = 0; i < cnt; ++i) {
  20.                 last = q.poll();
  21.                 if (last.left != null) q.offer(last.left);
  22.                 if (last.right != null) q.offer(last.right);
  23.             }
  24.             res.add(last.val);
  25.         }
  26.         return res;
  27.     }
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top