https://leetcode.com/problems/add-one-row-to-tree/

Source


    TreeNode* addOneRow(TreeNode* root, int val, int depth) {
        if (1 == depth) {
            TreeNode* node = new TreeNode(val, root, nullptr);
            root = node;
        }
        else {
            DFS(root, val, depth-1);
        }
        return root;
    }

    void DFS(TreeNode* cur, const int val, const int depth) {
        if (nullptr == cur) return;
        if (1 == depth) {
            TreeNode* node = new TreeNode(val, cur->left, nullptr);
            cur->left = node;
            node = new TreeNode(val, nullptr, cur->right);
            cur->right= node;
        }

        if (nullptr != cur->left) DFS(cur->left, val, depth-1);
        if (nullptr != cur->right) DFS(cur->right, val, depth-1);
    }

GitHub

AddOneRow