classSolution{public:// Very simliar to 94. Binary Tree Inorder TraversalintgetMinimumDifference(TreeNode*root){intans=INT_MAX;intprev=-1;stack<TreeNode*>stack;while(root||!stack.empty()){while(root){stack.push(root);root=root->left;}root=stack.top(),stack.pop();if(prev>=0)ans=min(ans,root->val-prev);prev=root->val;root=root->right;}returnans;}};
classSolution{// Very simliar to 94. Binary Tree Inorder TraversalpublicintgetMinimumDifference(TreeNoderoot){intans=Integer.MAX_VALUE;intprev=-1;Deque<TreeNode>stack=newArrayDeque<>();while(root!=null||!stack.isEmpty()){while(root!=null){stack.push(root);root=root.left;}root=stack.pop();if(prev>=0)ans=Math.min(ans,root.val-prev);prev=root.val;root=root.right;}returnans;}}