623. Add One Row to Tree
肥肥用BFS
用recursive的都是觸手吧
剩肥肥我又臭又長了
TreeNode* addOneRow(TreeNode* root, int val, int depth) {
queue<pair<TreeNode*,int>> q;
q.push({root, 1});
while(!q.empty())
{
TreeNode* cur = q.front().first;
int cur_level = q.front().second;
q.pop();
if(cur_level == depth-1)
{
TreeNode* node_left = new TreeNode(val);
TreeNode* node_right = new TreeNode(val);
node_left->left = cur->left;
node_right->right = cur->right;
cur->left = node_left;
cur->right = node_right;
}
else
{
if(cur->left)
q.push({cur->left, cur_level+1});
if(cur->right)
q.push({cur->right, cur_level+1});
}
}
if(depth == 1)
{
TreeNode* new_root = new TreeNode(val, root, NULL);
return new_root;
}
return root;
}