Clear C++


  • 0
    Z
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        TreeNode* addOneRow(TreeNode* root, int v, int d) {
            TreeNode * result= addARow(root, v, d, true);
            return result;
            
        }
    private: 
        TreeNode* addARow(TreeNode* root, int v, int d, bool isLeft) {
    		d--;
    		if (!root && d == 0) {
    			return new TreeNode(v);
    		}
    		if (!root) return NULL;
    		if (d == 0) {
    			TreeNode * node = new TreeNode(v);
    			if (isLeft) node->left = root;
    			else    node->right = root;
    			return node;
    		}
    		else {
    			root->left = addARow(root->left, v, d, true);
    			root->right = addARow(root->right, v, d, false);
                return root;
    		}
    		
    
    	}
            
    };
    

Log in to reply
 

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