diff --git a/14-Queue/21-RightSideView/Makefile b/14-Queue/21-RightSideView/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..f5f5dddd980cdd835006e560a694720fd45b1052 --- /dev/null +++ b/14-Queue/21-RightSideView/Makefile @@ -0,0 +1,51 @@ +CC := g++ + +#注意每行后面不要有空格,否则会算到目录名里面,导致问题 +SRC_DIR = ./ +BUILD_DIR = tmp +OBJ_DIR = $(BUILD_DIR)/obj +DEPS_DIR = $(BUILD_DIR)/deps + +#这里添加其他头文件路径 +INC_DIR = \ + -I./include \ + -I./src \ + +#这里添加编译参数 +CC_FLAGS := $(INC_DIR) -g -std=c++11 +LNK_FLAGS := \ + -L/usr/local/lib + +#这里递归遍历3级子目录 +DIRS := $(shell find $(SRC_DIR) -maxdepth 3 -type d) + +#将每个子目录添加到搜索路径 +VPATH = $(DIRS) + +#查找src_dir下面包含子目录的所有cpp文件 +#SOURCES = $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp)) +SOURCES = RightSideView.cpp +OBJS = $(addprefix $(OBJ_DIR)/,$(patsubst %.cpp,%.o,$(notdir $(SOURCES)))) +DEPS = $(addprefix $(DEPS_DIR)/, $(patsubst %.cpp,%.d,$(notdir $(SOURCES)))) +TARGET := $(patsubst %.cpp, %, $(SOURCES)) + + +$(TARGET):$(OBJS) + $(CC) $^ $(LNK_FLAGS) -o $@ +#编译之前要创建OBJ目录,确保目录存在 +$(OBJ_DIR)/%.o:%.cpp + if [ ! -d $(OBJ_DIR) ]; then mkdir -p $(OBJ_DIR); fi;\ + $(CC) -c $(CC_FLAGS) -o $@ $< +#编译之前要创建DEPS目录,确保目录存在 +#前面加@表示隐藏命令的执行打印 +$(DEPS_DIR)/%.d:%.cpp + @if [ ! -d $(DEPS_DIR) ]; then mkdir -p $(DEPS_DIR); fi;\ + set -e; rm -f $@;\ + $(CC) -MM $(CC_FLAGS) $< > $@.$$$$;\ + sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' < $@.$$$$ > $@;\ + rm -f $@.$$$$ +#前面加-表示忽略错误 +-include $(DEPS) +.PHONY : clean +clean: + rm -rf $(BUILD_DIR) $(TARGET) diff --git a/14-Queue/21-RightSideView/RightSideView-leetcode.cpp b/14-Queue/21-RightSideView/RightSideView-leetcode.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edd5e407275caeb008b76908590eb39d7a8681aa --- /dev/null +++ b/14-Queue/21-RightSideView/RightSideView-leetcode.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + vector rightSideView(TreeNode* root) { + if (root == nullptr) return {}; + vector res; + deque s; + s.push_back(root); + + while (!s.empty()) + { + int n = s.size(); + res.push_back(s.back()->val); + for (int i = 0; i < n; ++i) + { + TreeNode* node = s.front(); s.pop_front(); + if (node->left) s.push_back(node->left); + if (node->right) s.push_back(node->right); + } + } + return res; + } +}; + +void trimLeftTrailingSpaces(string &input) { + input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) { + return !isspace(ch); + })); +} + +void trimRightTrailingSpaces(string &input) { + input.erase(find_if(input.rbegin(), input.rend(), [](int ch) { + return !isspace(ch); + }).base(), input.end()); +} +//举例:[1,2,3,null,5,null,4] +TreeNode* stringToTreeNode(string input) { + trimLeftTrailingSpaces(input); + trimRightTrailingSpaces(input); + input = input.substr(1, input.length() - 2);//去掉[1,2,3,null,5,null,4]中的前面的[和最后的],变为:1,2,3,null,5,null,4; + if (!input.size()) { + return nullptr; + } + + string item; + stringstream ss; + ss.str(input); + + getline(ss, item, ','); //取出root对应的第一个字符'1'存入item; + cout << "item =" << item << endl; + TreeNode* root = new TreeNode(stoi(item)); + queue nodeQueue; + nodeQueue.push(root); + + while (true) { //层次遍历创建二叉树 + TreeNode* node = nodeQueue.front(); + nodeQueue.pop(); + + if (!getline(ss, item, ',')) { //读取左孩子的字符; + break; + } + //cout << "item =" << item << endl; + trimLeftTrailingSpaces(item); + if (item != "null") { + int leftNumber = stoi(item); + node->left = new TreeNode(leftNumber); //创建左孩子 + nodeQueue.push(node->left); + } + + if (!getline(ss, item, ',')) { //读取右孩子的字符; + break; + } + + trimLeftTrailingSpaces(item); + if (item != "null") { + int rightNumber = stoi(item); + node->right = new TreeNode(rightNumber); //创建右孩子 + nodeQueue.push(node->right); + } + } + return root; +} + +string integerVectorToString(vector list, int length = -1) { + if (length == -1) { + length = list.size(); + } + + if (length == 0) { + return "[]"; + } + + string result; + for(int index = 0; index < length; index++) { + int number = list[index]; + result += to_string(number) + ", "; + } + return "[" + result.substr(0, result.length() - 2) + "]"; +} + +int main() { + string line; + while (getline(cin, line)) { + TreeNode* root = stringToTreeNode(line); + + vector ret = Solution().rightSideView(root); + + string out = integerVectorToString(ret); + cout << out << endl; + } + return 0; +} diff --git a/14-Queue/21-RightSideView/RightSideView.cpp b/14-Queue/21-RightSideView/RightSideView.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4cb160cf48803618beb6c1c6595972a6e2b8b6e8 --- /dev/null +++ b/14-Queue/21-RightSideView/RightSideView.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; + +class Solution { +public: + vector rightSideView(TreeNode* root) { + unordered_map rightmostValueAtDepth; + int max_depth = -1; + queue nodeQueue; + queue depthQueue; + nodeQueue.push(root); + depthQueue.push(0); + + while(!nodeQueue.empty()) { + TreeNode *node = nodeQueue.front(); + nodeQueue.pop(); + int depth = depthQueue.front(); + depthQueue.pop(); + + if(node != nullptr) { + max_depth = max(max_depth, depth); + rightmostValueAtDepth[depth] = node->val; + + nodeQueue.push(node->left); + depthQueue.push(depth+1); + nodeQueue.push(node->right); + depthQueue.push(depth+1); + } + } + vector rightView; + for(int depth = 0; depth <= max_depth; ++depth) { + rightView.push_back(rightmostValueAtDepth[depth]); + } + return rightView; + } +}; + +void trimLeftTrailingSpaces(string &input) { + input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) { + return !isspace(ch); + })); +} + +void trimRightTrailingSpaces(string &input) { + input.erase(find_if(input.rbegin(), input.rend(), [](int ch) { + return !isspace(ch); + }).base(), input.end()); +} +//举例:[1,2,3,null,5,null,4] +TreeNode* stringToTreeNode(string input) { + trimLeftTrailingSpaces(input); + trimRightTrailingSpaces(input); + input = input.substr(1, input.length() - 2);//去掉[1,2,3,null,5,null,4]中的前面的[和最后的],变为:1,2,3,null,5,null,4; + if (!input.size()) { + return nullptr; + } + + string item; + stringstream ss; + ss.str(input); + + getline(ss, item, ','); //取出root对应的第一个字符'1'存入item; + cout << "item =" << item << endl; + TreeNode* root = new TreeNode(stoi(item)); + queue nodeQueue; + nodeQueue.push(root); + + while (true) { //层次遍历创建二叉树 + TreeNode* node = nodeQueue.front(); + nodeQueue.pop(); + + if (!getline(ss, item, ',')) { //读取左孩子的字符; + break; + } + //cout << "item =" << item << endl; + trimLeftTrailingSpaces(item); + if (item != "null") { + int leftNumber = stoi(item); + node->left = new TreeNode(leftNumber); //创建左孩子 + nodeQueue.push(node->left); + } + + if (!getline(ss, item, ',')) { //读取右孩子的字符; + break; + } + + trimLeftTrailingSpaces(item); + if (item != "null") { + int rightNumber = stoi(item); + node->right = new TreeNode(rightNumber); //创建右孩子 + nodeQueue.push(node->right); + } + } + return root; +} + +string integerVectorToString(vector list, int length = -1) { + if (length == -1) { + length = list.size(); + } + + if (length == 0) { + return "[]"; + } + + string result; + for(int index = 0; index < length; index++) { + int number = list[index]; + result += to_string(number) + ", "; + } + return "[" + result.substr(0, result.length() - 2) + "]"; +} + +int main() { + string line; + while (getline(cin, line)) { + TreeNode* root = stringToTreeNode(line); + + vector ret = Solution().rightSideView(root); + + string out = integerVectorToString(ret); + cout << out << endl; + } + return 0; +}