leetcode

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

0623.cpp (738B)


0 class Solution { 1 public: 2 TreeNode *addOneRow(TreeNode *root, int val, int depth) const { 3 if (depth == 1) return new TreeNode(val, root, nullptr); 4 queue<TreeNode *> q; 5 q.push(root); 6 for (int lvl = 2; !q.empty(); lvl++) { 7 for (int k = q.size(); k > 0; k--) { 8 TreeNode *root = q.front(); 9 q.pop(); 10 if (root->left) q.push(root->left); 11 if (root->right) q.push(root->right); 12 if (lvl == depth) { 13 root->left = new TreeNode(val, root->left, nullptr); 14 root->right = new TreeNode(val, nullptr, root->right); 15 } 16 } 17 } 18 return root; 19 } 20 };