//关键注意 d == 1的情况。
class Solution {
public:
TreeNode* addOneRow(TreeNode* root, int v, int d) {
if(root == NULL)
return new TreeNode(v);
if(d == 1) {
TreeNode* newRoot = new TreeNode(v);
newRoot->left = root;
return newRoot;
}
addRow(root,v,d,1);
return root;
}
void addRow(TreeNode* root, int val, int target, int cur) {
if(cur == target-1) {
TreeNode* left = root->left;
TreeNode* right = root->right;
TreeNode* curLeft = new TreeNode(val);
TreeNode* curRight = new TreeNode(val);
root->left = curLeft;
root->right = curRight;
curLeft ->left = left;
curRight->right = right;
return;
}
if(root->left)
addRow(root->left,val,target,cur+1);
if(root->right)
addRow(root->right,val,target,cur+1);
}
};