From b00c63101da17019ac489c378861de6707bf515c Mon Sep 17 00:00:00 2001 From: willrlzhang Date: Thu, 16 Sep 2021 11:48:31 +0800 Subject: [PATCH 1/2] feat: add cpp solution to lcof2 problem: NO.046 --- .../README.md" | 52 +++++++++++++++++++ .../Solution.cpp" | 44 ++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 "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" 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..c844b0edfefe0 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,58 @@ ``` +### **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; + } +}; From 39a185e78aaeda05667396a834367d80aa299b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=8A=AD=E7=8F=91?= <382084620@qq.com> Date: Thu, 16 Sep 2021 16:59:20 +0800 Subject: [PATCH 2/2] format README.md --- .../README.md" | 3 --- 1 file changed, 3 deletions(-) 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 c844b0edfefe0..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" @@ -114,11 +114,8 @@ public: return res; } }; - ``` - - ### **...** ```