https://leetcode.com/problems/add-one-row-to-tree/description/
void add(TreeNode* node, const int& v, const int& d, int dep) {
if(!node) return;
if(d == dep){
TreeNode* save;
save = node->left;
node->left = new TreeNode(v);
node->left->left = save;
save = node->right;
node->right = new TreeNode(v);
node->right->right = save;
return;
}
add(node->left, v, d, dep+1);
add(node->right, v, d, dep+1);
}
TreeNode* addOneRow(TreeNode* root, int v, int d) {
if(d==1) {
TreeNode* save = root;
root = new TreeNode(v);
root->left = save;
return root;
}
add(root, v, d, 2);
return root;
}
No comments:
Post a Comment