memory (log(n)) time (o(n)) c++ solution


  • 0

    class Solution {
    public:
    int getMinimumDifference(TreeNode* root) {
    int min_diff = INT_MAX;
    int past = -1;
    stack<TreeNode*> stk;
    TreeNode* cur = root;
    while(cur){
    stk.push(cur);
    cur = cur->left;
    }
    while(!stk.empty()){
    TreeNode* top = stk.top();
    stk.pop();
    TreeNode* tmp = top->right;
    cout << top->val << endl;
    while(tmp){
    stk.push(tmp);
    tmp = tmp->left;
    }
    if(past != - 1){
    min_diff = min(min_diff, abs(past - top->val));
    }
    past = top->val;
    }
    return min_diff;
    }
    };


Log in to reply
 

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