首页>itarticle>【leetcode 199】binary tree right side view 二叉树的右视图
【leetcode 199】binary tree right side view 二叉树的右视图
admin1月 23, 20210
“The Linux philosophy is “Laugh in the face of danger”.Oops.Wrong One. “Do it yourself”. Yes, that”s it.” Linux的哲学就是“在危险面前放声大笑”,呵呵,不是这句,应该是“一切靠自己,自力更生”才对。
* Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ publicstatic List<Integer> rightSideView(TreeNode root){ List<Integer> res = new LinkedList<>(); if (root == null) return res; Queue<TreeNode> queue = new LinkedList<>(); int size = 1; queue.offer(root); while (!queue.isEmpty()) { res.add((((LinkedList<TreeNode>) queue).peekLast().val)); for (int i = 0; i < size; i++) { TreeNode r = queue.poll(); if (r.left != null) queue.offer(r.left); if (r.right != null) queue.offer(r.right); } size = queue.size(); } return res; } }
近期评论