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 199. Binary Tree Right Side View
原文地址:http://blog.csdn.net/stephen_wong/article/details/46275057