其实题目的意思就是相当于二叉树每一行的最右边的一个元素,那么简单,先bfs后取每一个数组的最后一位数就可以了,代码如下: 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(NU...
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]. 本题难度...
英文网站:199. Binary Tree Right Side View 中文网站:199. 二叉树的右视图 问题描述 Given therootof a binary tree, imagine yourself standing on theright sideof it, returnthe values of the nodes you can see ordered from top to bottom. Example 1: Input: root = [1,2,3,null,5,null,4] O...
q.push(root);while(!q.empty()) { res.push_back(q.back()->val);intsize =q.size();for(inti =0; i < size; ++i) { TreeNode*node =q.front(); q.pop();if(node->left) q.push(node->left);if(node->right) q.push(node->right); } }returnres; } }; LeetCode All in One...
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 <--- / \ ...
参考LeetCode #102 Binary Tree Level Order Traversal 二叉树的层序遍历 层次遍历按层输出最右边的结点 时间复杂度O(n), 空间复杂度O(n) 代码: C++: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
【leetcode】Binary Tree Right Side View(middle) Given a binary tree, imagine yourself standing on therightside of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree,...
http://www.programcreek.com/2014/04/leetcode-binary-tree-right-side-view-java/ 看了这篇博客就懂了。 ** 总结: queue来遍历tree ** Anyway, Good luck, Richardo! My code: /** * Definition for a binary tree node. * public class TreeNode { ...
Leetcode 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,
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. Example: Input: [1,2,3,null,5,null,4]Output: [1,3,4]Explanation:1<---/ \23<---\ \54<--- ...