C++ ,NOT RECURSION


  • 0
    W
    class Solution {
    public:
        int findSecondMinimumValue(TreeNode* root) {
            stack<TreeNode*> s;
            TreeNode* p = root;
            int minst =  p->val, ans = INT_MAX;
            while(p != nullptr || !s.empty()) {
            	while(p) {
            		s.push(p);
            		p = p->left;
    			}
    			if(!s.empty()) {
    				p = s.top();
    				s.pop();
                    if(p->val < ans && p->val > minst)
                        ans = p->val;
    				p = p->right;
    			}
    		}
            return ans == INT_MAX ? -1 : ans;
        }
    };
    

Log in to reply
 

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