1. 程式人生 > >使用leetcode輪子快速實現樹的三種遍歷

使用leetcode輪子快速實現樹的三種遍歷

樹的前中後序遍歷


最近發現,leetcode的除錯面板真是個神器,可以新增各種函式
在這裡插入圖片描述


最近想實現樹的三種遍歷,想想自己寫輸入輸出函式就累(對不起我不是合格程式設計師orz 嚶嚶嚶於是就到leetcode上抄了函式改了下,看了下實現方式其實也不是很難(站著說話ing))

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>
#include
<sstream>
using namespace std; /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //[a,b,c,d,e,f,null,null,g] struct TreeNode { int val; TreeNode *
left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: vector<int> preorderTraversal(TreeNode *root) { vector<int> res; std::stack<TreeNode *> temp; while (root || !temp.empty()) { while
(root) { temp.push(root); res.push_back(root->val); root = root->left; } root = temp.top(); temp.pop(); root = root->right; } return res; } vector<int> inorderTraversal(TreeNode *root) { vector<int> res; std::stack<TreeNode *> temp; while (root || !temp.empty()) { while (root) { temp.push(root); root = root->left; } root = temp.top(); temp.pop(); res.push_back(root->val); root = root->right; } return res; } vector<int> postorderTraversal(TreeNode *root) { vector<int> res; std::stack<TreeNode *> temp; while (root || !temp.empty()) { while (root) { temp.push(root); res.insert(res.begin(), root->val); root = root->right; } root = temp.top(); temp.pop(); root = root->left; } 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()); } TreeNode *stringToTreeNode(string input) { trimLeftTrailingSpaces(input); trimRightTrailingSpaces(input); input = input.substr(1, input.length() - 2); if (!input.size()) { return nullptr; } string item; stringstream ss; ss.str(input); getline(ss, item, ','); 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; } 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(int argc, char const *argv[]) { string line; cout << "請輸入您的節點,以中括號開始和結束.如:[a,b,c,d,e,f,null,null,g](a到g代表數字).\n以#開頭表示退出." << endl; while (getline(cin, line)) { if (line[0] == '#') { break; } TreeNode *root = stringToTreeNode(line); cout << "請選擇您要進行的操作" << endl; cout << "\t\t1.前序遍歷\n\t\t2.中序遍歷\n\t\t3.後序遍歷\n\t\t0.退出\n"; while (getline(cin, line)) { int choose=stoi(line); if (choose == 0) { break; } vector<int> ret; switch (choose) { case 1: { ret = Solution().preorderTraversal(root); break; } case 2: { ret = Solution().inorderTraversal(root); break; } case 3: { ret = Solution().postorderTraversal(root); break; } } string out = integerVectorToString(ret); cout << out << endl; } } system("pause"); return 0; }