给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:
输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:
1 <---
/ \
2 3 <---
\ \
5 4 <---
考察二叉树的遍历,迭代法借助队列实现
递归实现:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> res;
dfs(root, res, 0);
return res;
}
void dfs(TreeNode* curr, vector<int>& res, int currDepth) {
if(curr == NULL) {
return;
}
if(currDepth == res.size()) {
res.push_back(curr->val);
}
dfs(curr->right, res, currDepth + 1);
dfs(curr->left, res, currDepth + 1);
}
};
迭代法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> res;
if(root == NULL) {
return res;
}
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
res.push_back(q.back()->val);
for(int i = q.size(); i > 0; i--) {
TreeNode* temp = q.front();
q.pop();
if(temp->left) {
q.push(temp->left);
}
if(temp->right) {
q.push(temp->right);
}
}
}
return res;
}
};