提交 3526f6cb 编写于 作者: cosmicing's avatar cosmicing

更新14-Queue/21-RightSideView/Makefile,...

更新14-Queue/21-RightSideView/Makefile, 14-Queue/21-RightSideView/RightSideView.cpp, 14-Queue/21-RightSideView/RightSideView-leetcode.cpp
上级 c1f74511
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)
#include<iostream>
#include<string>
#include<string.h>
#include<stack>
#include<assert.h>
#include<queue>
#include<unordered_map>
#include<algorithm>
#include<sstream>
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<int> rightSideView(TreeNode* root) {
if (root == nullptr) return {};
vector<int> res;
deque<TreeNode*> 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<TreeNode*> 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<int> 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<int> ret = Solution().rightSideView(root);
string out = integerVectorToString(ret);
cout << out << endl;
}
return 0;
}
#include<iostream>
#include<string>
#include<string.h>
#include<stack>
#include<assert.h>
#include<queue>
#include<unordered_map>
#include<algorithm>
#include<sstream>
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<int> rightSideView(TreeNode* root) {
unordered_map<int, int> rightmostValueAtDepth;
int max_depth = -1;
queue<TreeNode*> nodeQueue;
queue<int> 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<int> 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<TreeNode*> 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<int> 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<int> ret = Solution().rightSideView(root);
string out = integerVectorToString(ret);
cout << out << endl;
}
return 0;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册