199. Binary Tree Right Side View

题目:

Given 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.

For example:
Given the following binary tree,

   1            <---
 /   2     3         <---
 \       5     4       <---

You should return [1, 3, 4].

链接: http://leetcode.com/problems/binary-tree-right-side-view/

6/25/2017

2ms, 31%

注意,

1. linkedlist的几个常见方法

2. added可以不需要,只需要判断i== 0就可以了

 1 public class Solution {
 2     public List<Integer> rightSideView(TreeNode root) {
 3         List<Integer> ret = new ArrayList<Integer>();
 4         if (root == null) {
 5             return ret;
 6         }
 7
 8         Queue<TreeNode> queue = new LinkedList<TreeNode>();
 9         queue.offer(root);
10         while (!queue.isEmpty()) {
11             boolean added = false;
12             int size = queue.size();
13             for (int i = 0; i < size; i++) {
14                 TreeNode tmp = queue.poll();
15                 if (!added) {
16                     ret.add(tmp.val);
17                     added = true;
18                 }
19                 if (tmp.right != null) {
20                     queue.add(tmp.right);
21                 }
22                 if (tmp.left != null) {
23                     queue.add(tmp.left);
24                 }
25             }
26         }
27         return ret;
28     }
29 }

别人的算法

recursive,但是很聪明的算法,尤其是判断是否要加入result的时候

https://discuss.leetcode.com/topic/11768/my-simple-accepted-solution-java

devide & conquer,没看懂

https://discuss.leetcode.com/topic/11302/java-solution-using-divide-and-conquer

Python

https://discuss.leetcode.com/topic/16164/5-9-lines-python-48-ms

更多讨论

https://discuss.leetcode.com/category/207/binary-tree-right-side-view

时间: 2024-10-15 05:07:34

199. Binary Tree Right Side View的相关文章

&lt;LeetCode OJ&gt; 199. Binary Tree Right Side View

Total Accepted: 40438 Total Submissions: 117654 Difficulty: Medium Given 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. For example: Given the following binary

【LeetCode】199. Binary Tree Right Side View

Binary Tree Right Side View Given 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. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- Y

LeetCode OJ:Binary Tree Right Side View(右侧视角下的二叉树)

Given 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. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3, 4].

LeetCode OJ:Binary Tree Right Side View

题目: Given 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. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3,

LeetCode OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!! 下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || po

LeetCode 199. Binary Tree Right Side View

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> rightSideView

8.8 LeetCode 199 Binary Tree Right Side View

Question link Given 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. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should retu

[LeetCode] 199. Binary Tree Right Side View Java

题目: Given 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. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3,

leetcode 199. Binary Tree Right Side View 求所能看到的叶子节点 ---------- java

Given 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. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3, 4].