leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0919.cpp (797B)


0 class CBTInserter { 1 TreeNode *root = nullptr; 2 queue<TreeNode *> q; 3 4 public: 5 CBTInserter(TreeNode *root) : root(root), q({root}) {} 6 TreeNode *get_root() { return root; } 7 8 int insert(int val) { 9 TreeNode *node = new TreeNode(val); 10 while (!q.empty()) { 11 TreeNode *root = q.front(); 12 if (root->left) 13 q.push(root->left); 14 else { 15 root->left = node; 16 q.push(root->left); 17 return root->val; 18 } 19 20 q.pop(); 21 22 if (root->right) 23 q.push(root->right); 24 else { 25 root->right = node; 26 q.push(root->right); 27 return root->val; 28 } 29 } 30 31 return -1; 32 } 33 };