Last active
July 15, 2024 18:07
-
-
Save gibo/97ac43dc2e0c061c306648319d090c84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A | |
/ \ | |
B C | |
/ \ / \ | |
D E F G | |
*/ | |
function Node(id, value, left, right) { | |
this.id = id | |
this.value = value | |
this.left = left | |
this.right = right | |
} | |
// the tree is constructed bottom up | |
// ie. rotate the above diagram 180 degrees | |
// left | |
const dNode = new Node('D', 'dup') | |
const eNode = new Node('E', 'dup') | |
const bNode = new Node('B', 'unique1', dNode, eNode) | |
// right | |
const fNode = new Node('F', 'unique2') | |
const gNode = new Node('G', 'unique3') | |
const cNode = new Node('B', 'unique4', fNode, gNode) | |
// root | |
const aNode = new Node('A', 'unique5', bNode, cNode); | |
const output = new Set(); | |
function dfs(root){ | |
if(root === null) return | |
output.add(root.value) | |
if(root.left) dfs(root.left) | |
if(root.right) dfs(root.right) | |
} | |
dfs(aNode) | |
console.log('> output', output); // should have all 'unique{X}', and one 'dup' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment