diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/README.md" index 980428046278e..e85f50900100b 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/README.md" @@ -67,6 +67,55 @@ ``` +### **C++** + +```cpp +class Solution +{ +public: + vector rightSideView ( TreeNode* root ) + { + vector res; + + if ( !root ) + { + return res; + } + + queue que; + que.push ( root ); + + while ( !que.empty() ) + { + int size = que.size(); + + for ( int i = 0; i < size; i++ ) + { + TreeNode* ptr = que.front(); + que.pop(); + + if ( i == size - 1 ) + { + res.push_back ( ptr->val ); + } + + if ( ptr->left ) + { + que.push ( ptr->left ); + } + + if ( ptr-> right ) + { + que.push ( ptr->right ); + } + } + } + + return res; + } +}; +``` + ### **...** ``` diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/Solution.cpp" new file mode 100644 index 0000000000000..3905e1ef9ae7b --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/Solution.cpp" @@ -0,0 +1,44 @@ +class Solution +{ +public: + vector rightSideView ( TreeNode* root ) + { + vector res; + + if ( !root ) + { + return res; + } + + queue que; + que.push ( root ); + + while ( !que.empty() ) + { + int size = que.size(); + + for ( int i = 0; i < size; i++ ) + { + TreeNode* ptr = que.front(); + que.pop(); + + if ( i == size - 1 ) + { + res.push_back ( ptr->val ); + } + + if ( ptr->left ) + { + que.push ( ptr->left ); + } + + if ( ptr-> right ) + { + que.push ( ptr->right ); + } + } + } + + return res; + } +};