DEV Community

Discussion on: Daily Coding Problem #3

themindfuldev profile image
Tiago Romero Garcia • Edited on

In Javascript, JSON serialization/unserialization is already supported, so this felt like cheating:

class Node {
    constructor(val, left = null, right = null) {
        this.val = val;
        this.left = left;
        this.right = right;

    toString() {
        return JSON.stringify(this);

const serialize = node => node.toString();
const deserialize = string => JSON.parse(string);

const node = new Node('root', new Node('left', new Node('left.left')), new Node('right'));

console.log(deserialize(serialize(node)).left.left.val); // prints "left.left"

Enter fullscreen mode Exit fullscreen mode
leviermonaites profile image
Levi Ermonaites De Freitas

Exactly man, I looked at it, and thought, what?? They're asking one thing that already exists?