Simple C++ solution


  • 2
    S
    class Solution {
    public:
        void addOneRowR(TreeNode* root, int v, int d, int cd) {
            if (!root) return;
            
            if (cd == d) {
                TreeNode *savLeft = root->left;
                root->left = new TreeNode(v);
                root->left->left = savLeft;
                
                TreeNode *savRight = root->right;
                root->right = new TreeNode(v);
                root->right->right = savRight;
                return;
            }
            addOneRowR(root->left, v, d, cd + 1);
            addOneRowR(root->right, v, d, cd + 1);
        }
        TreeNode* addOneRow(TreeNode* root, int v, int d) {
            if (d == 1) {
                TreeNode *newRoot = new TreeNode(v);
                newRoot->left = root;
                return newRoot;
            }
            addOneRowR(root, v, d, 2);
            return root;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.