ð 199. äºåæ çå³è§åŸ
2022幎10æ10æ¥
- algorithm
ð 199. äºåæ çå³è§åŸ
éŸåºŠ: ð
é®é¢æè¿°
解æ³
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
// æè·¯ïŒ
// å±åº
List<Integer> res = new ArrayList<>();
if(root == null) {
return res;
}
// root != null
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
int len = queue.size();
for(int i = 0; i < len; i ++) {
TreeNode cur = queue.poll();
if(i == len - 1) {
res.add(cur.val);
}
if(cur.left != null) {
queue.offer(cur.left);
}
if(cur.right != null) {
queue.offer(cur.right);
}
}
}
return res;
}
}