Skip to content

Instantly share code, notes, and snippets.

@marklin-latte
Last active December 14, 2017 09:20
Show Gist options
  • Save marklin-latte/9998a13d19f70191a7f9754918e25d2b to your computer and use it in GitHub Desktop.
Save marklin-latte/9998a13d19f70191a7f9754918e25d2b to your computer and use it in GitHub Desktop.
Second Minimum Node In a Binary Tree (未整理)
var findSecondMinimumValue = function(root) {
let stack = [];
let pre_val = 999999;
let temp_count = 1;
let result = 0;
stack.push(root);
let current;
while (stack.length > 0) {
current = stack.pop();
if(current.val > root.val){
if(pre_val > current.val){
temp_count--;
pre_val = current.val;
result = current.val;
}else{
if(!result){
pre_val = current.val;
}
}
}
// if(temp_count > 0){
// if(temp_val !== current.val ){
// temp_count--;
// temp_val = current.val;
// if(temp_count === 0){
// return current.val;
// }
// }
// }else{
// return current.val;
// }
if(current.right){
stack.push(current.right);
}
if(current.left){
stack.push(current.left);
}
}
if(temp_count > 0){
return -1;
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment