LeetCode-199.Binary Tree Right Side View
2016-05-30 22:04
417 查看
https://leetcode.com/problems/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,
You should return
很简单的一道题,跟一层层遍历树的题型一样
C++实现
#include <vector>
#include <queue>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
vector<int> rightSideView(TreeNode* root)
{
vector<int> res;
if (!root)
return res;
queue<TreeNode*> q;
q.push(root);
int n;
TreeNode* node;
while ((n = q.size()) > 0)
{
res.push_back(q.front()->val);
for (int i = 0; i < n; i++)
{
node = q.front();
q.pop();
if (node->right)
q.push(node->right);
if (node->left)
q.push(node->left);
}
}
return res;
}
递归解,参考https://leetcode.com/discuss/31348/my-simple-accepted-solution-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].
很简单的一道题,跟一层层遍历树的题型一样
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public class Solution { public IList<int> RightSideView(TreeNode root) { IList<int> res = new List<int>(); if (root == null) return res; Queue<TreeNode> q = new Queue<TreeNode>(); q.Enqueue(root); int n; TreeNode node; while ((n=q.Count)>0) { res.Add(q.Peek().val); for (int i = 0; i < n; i++) { node = q.Dequeue(); if (node.right != null) q.Enqueue(node.right); if (node.left != null) q.Enqueue(node.left); } } return res; } }
C++实现
#include <vector>
#include <queue>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
vector<int> rightSideView(TreeNode* root)
{
vector<int> res;
if (!root)
return res;
queue<TreeNode*> q;
q.push(root);
int n;
TreeNode* node;
while ((n = q.size()) > 0)
{
res.push_back(q.front()->val);
for (int i = 0; i < n; i++)
{
node = q.front();
q.pop();
if (node->right)
q.push(node->right);
if (node->left)
q.push(node->left);
}
}
return res;
}
递归解,参考https://leetcode.com/discuss/31348/my-simple-accepted-solution-java
public class Solution { public IList<int> RightSideView(TreeNode root) { IList<int> res = new List<int>(); Func(res, root, 0); return res; } private void Func(IList<int> res, TreeNode root, int level) { if (root == null) return; if (level == res.Count) res.Add(root.val); Func(res,root.right,level + 1); Func(res,root.left,level + 1); } }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解