simple c++ solution


  • 0
    B
        TreeNode *addOneRow(TreeNode *root, int v, int d, bool left = true) {
            if (d == 1) {
                TreeNode *node = new TreeNode(v);
                if (left) node->left = root;
                else node->right = root;
                return node;
            }
            
            if(root) {
                root->left = addOneRow(root->left, v, d - 1, true);
                root->right = addOneRow(root->right, v, d - 1, false);
            }
        
            return root;
        }
    

    not much different that the top voted ones, except an additional parameter to indicate if the current node is from left or right.


Log in to reply
 

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