LeetCode 199. Binary Tree Right Side View

BFS, 注意要先对右节点调用BFS即可。


代码:

class Solution 
{
public:
    vector<int> rightSideView(TreeNode *root) 
    {
        bfs(root, 1);
        return ret;
    }
private:
	void bfs(TreeNode* node, int level)
	{
		if (node == nullptr)
		{
			return ;
		}
		if (ret.size() < level)
		{
			ret.push_back(node->val);
		}
		bfs(node->right, level+1);
		bfs(node->left, level+1);
	}
	vector<int> ret;
};


你可能感兴趣的:(LeetCode,C++)