Build Binary Search Tree
来源:互联网 发布:苏黎世大学留学 知乎 编辑:程序博客网 时间:2024/06/05 23:45
/* LeetCode 536. Construct Binary Tree from String* * You need to construct a binary tree from a string consisting of parenthesis and integers.* * The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure.* * You always start to construct the left child node of the parent first if it exists.* * Example:* * Input: "4(2(3)(1))(6(5))"* Output: return the tree root node representing the following tree:* * 4 * / \ * 2 6 * / \ / * 3 1 5 */#include <iostream>#include <list>#include <unordered_set>#include <vector>#include <algorithm>#include <algorithm>#include <random>#include <functional>#include <string>#include <memory>#include <stdexcept> // std::out_of_range#include <stack>using namespace std;template <typename T>class binaryNode {public: /* default ctor */ binaryNode(): key(0), left(nullptr), right(nullptr), p(nullptr) {} /* ctor */ binaryNode(const binaryNode &n): key(n.key), left(n.left), right(n.right),p(n.p) {} binaryNode(const T& t):key(t), left(nullptr), right(nullptr), p(nullptr){} /* dtor */ ~binaryNode() = default;/* data member */public: binaryNode *left; binaryNode *right; binaryNode *p; T key;};vector<shared_ptr<binaryNode<int>>> vshes;vector<binaryNode<int>*> vptrs;char ms[100000];void buildTree(char *b, char *e, binaryNode<int>* root) { char *it = b; string mk; for (;it != e; ++it) { if (*it != '(') mk += *it; else break; } int key = stoi(mk); if (!root) { cerr << "Wriong: root is nil\n"; return; } root->key = key; if (it != e) {/*one child exist at least*/ string s1; it++; int num_l = 1; char *b1 = it, *b2, *e2; for (;it != e; ++it) { if (*it == '(') num_l++; else if (*it == ')') { num_l--; if (num_l == 0) { break; } } } static int m11 = 0;m11++; shared_ptr<binaryNode<int>> c0sh = make_shared<binaryNode<int>>(binaryNode<int>(0)); vshes.push_back(c0sh); shared_ptr<binaryNode<int>> c1sh = nullptr; char *e1 = it; c0sh->p = root; it++; if (it != e) {/* two */ it++; b2 = it; e2 = e-1; c1sh = make_shared<binaryNode<int>> (binaryNode<int>(0)); vshes.push_back(c1sh); } buildTree(b1,e1,c0sh.get()); c0sh->p = root; //try { /////////////// if (c1sh) { c1sh->p = root; buildTree(b2,e2,c1sh.get()); root->right = c1sh.get(); root->left = c0sh.get(); } else { root->left = c0sh.get(); root->right = nullptr; } } else { /* none child exists */ return; } return;}void printTree(binaryNode<int> *t) { if (t) { cout << t->key << " "; printTree(t->left); printTree(t->right); } else return;}int main() { string mstr; cin >> mstr; copy(mstr.begin(), mstr.end(), ms); ms[mstr.size()] = '\0'; auto p1 = make_shared<binaryNode<int>>(binaryNode<int>(0)); binaryNode<int>* root = p1.get(); buildTree(ms, ms+mstr.size(),root); printTree(root); return 0;}
0 0
- Build Binary Search Tree
- Build A Binary Search Tree
- Build A Binary Search Tree
- PAT 1099. Build A Binary Search Tree
- 1099.Build A Binary Search Tree
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- A1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- pat 1099 Build A Binary Search Tree
- 1099. Build A Binary Search Tree
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 函数fspecial( )与滤波算子
- IIS7中添加html对<! #include >的引用
- MyEclipse10安装SVN插件的几种方法
- PostgreSQL安装出现的一系列问题及最后解决方法
- 欢迎使用CSDN-markdown编辑器
- Build Binary Search Tree
- struts2项目中web.xml配置
- 特殊矩阵——稀疏矩阵
- 【LeetCode笔记】113. Path Sum II DFS搜索+记录节点
- SpriteKit中Node数量只增不降的原因和解决
- 腾讯云的使用(安装jdk和Tomcat)
- 494. Target Sum
- opencv学习(四十)之寻找图像轮廓findContours()
- 【SSLGZ 2644】线段树练习题一