Skip to content

Instantly share code, notes, and snippets.

@vadimkorr
Last active March 12, 2018 08:23
Show Gist options
  • Select an option

  • Save vadimkorr/7d3ea5d3790a87b98a277ea2224743d7 to your computer and use it in GitHub Desktop.

Select an option

Save vadimkorr/7d3ea5d3790a87b98a277ea2224743d7 to your computer and use it in GitHub Desktop.
Find a minimal value in binary tree
// Find a minimal value in binary tree
let node = function(data) {
this.data = data;
this.left = null;
this.right = null;
}
let findMin = function(root) {
let curr = root;
while(curr.left) {
curr = curr.left;
}
console.log('Min: ', curr.data);
}
// building following tree
// 5
// / \
// 3 6
// / \ \
// 1 4 8
// / \ / \
// 0 2 7 9
let root = new node(5);
root.left = new node(3);
root.right = new node(6);
root.left.left = new node(1);
root.left.right = new node(4);
root.right.right = new node(8);
root.left.left.left = new node(0);
root.left.left.right = new node(2);
root.right.right.left = new node(7);
root.right.right.right = new node(9);
findMin(root);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment