199二叉树的右视图(深度优先搜索、广度优先搜索)

1、题目描述

给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

2、示例

输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:

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

3、题解

解法一:

基本思想:深度优先搜索迭代法,使用栈分别记录节点以及对应节点深度,只有遍历的节点深度等于当前已遍历的最大深度才加入res

解法二:

基本思想:深度优先搜索递归法,从右子树开始不断遍历节点并且记录节点深度,只有遍历的节点深度等于当前已遍历的最大深度才加入res

解法三:

基本思想:广度优先搜索迭代法,将每一层的节点按照先进先出顺序保存到队列,将每一层最后一个节点值加入res

#include<iostream>
#include<algorithm>
#include<vector>
#include<deque>
#include<stack>
using namespace std;
struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
#define inf 9999
void Init_TreeNode(TreeNode** T, vector<int>& vec, int& pos)
{
	if (vec[pos] == inf || vec.size() == 0)
		*T = NULL;
	else
	{
		(*T) = new TreeNode(0);
		(*T)->val = vec[pos];
		Init_TreeNode(&(*T)->left, vec, ++pos);
		Init_TreeNode(&(*T)->right, vec, ++pos);
	}
}
class Solution {
public:
	vector<int> rightSideView(TreeNode* root) {
		//基本思想:深度优先搜索迭代法,使用栈分别记录节点以及对应节点深度
		//只有遍历的节点深度等于当前已遍历的最大深度才加入res
		stack<TreeNode*> nodeStack;
		stack<int> depthStack;
		vector<int> res;
		int curdepth = 0, preNodedepth = -1;
		while (root != NULL || !nodeStack.empty())
		{
			while (root != NULL)
			{
				nodeStack.push(root);
				depthStack.push(preNodedepth + 1);
				if (curdepth == depthStack.top())
				{
					res.push_back(root->val);
					curdepth++;
				}
				preNodedepth++;
				root = root->right;
			}
			root = nodeStack.top();
			nodeStack.pop();
			preNodedepth = depthStack.top();
			depthStack.pop();
			root = root->left;
		}
		return res;
	}
};
class Solution1 {
public:
	vector<int> res;
	int curdepth = 0;
	vector<int> rightSideView(TreeNode* root) {
		//基本思想:深度优先搜索递归法,从右子树开始不断遍历节点并且记录节点深度
		//只有遍历的节点深度等于当前已遍历的最大深度才加入res
		Recursion(root, 0);
		return res;
	}
	void Recursion(TreeNode* root, int depth)
	{
		if (root == NULL)
			return;
		if (curdepth == depth)
		{
			res.push_back(root->val);
			curdepth++;
		}
		Recursion(root->right, depth + 1);
		Recursion(root->left, depth + 1);
	}
};
class Solution2 {
public:
	vector<int> rightSideView(TreeNode* root) {
		//基本思想:广度优先搜索迭代法,将每一层的节点按照先进先出顺序保存到队列,将每一层最后一个节点值加入res
		vector<int> res;
		if (root == NULL)
			return res;
		deque<TreeNode*> queue;
		TreeNode* cur;
		vector<int> vec;
		queue.push_back(root);
		while (!queue.empty())
		{
			//将保存在queue中的当前层节点从队尾弹出保存至vec,弹出的同时将下一层的节点从队首入队列
			int len = queue.size();
			for (int i = 0; i < len; i++)
			{
				cur = queue.back();
				queue.pop_back();
				vec.push_back(cur->val);
				if (cur->left != NULL)
					queue.push_front(cur->left);
				if (cur->right != NULL)
					queue.push_front(cur->right);
			}
			res.push_back(vec.back());
			vec.clear();
		}
		return res;
	}
};
int main()
{
	Solution solute;
	TreeNode* root = NULL;
	vector<int> vec = { 0,2,1,5,inf,inf,7,inf,inf,inf,4,3,inf,6,inf,inf,-1,inf,8,inf,inf };
	int pos = 0;
	Init_TreeNode(&root, vec, pos);
	vector<int> res = solute.rightSideView(root);
	for_each(res.begin(), res.end(), [](const auto v) {cout << v << endl; });
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值