Given a binary tree defined as below, return the height of the tree.
class Node {
constructor(val) {
this.data = val;
this.left = null;
this.right = null;
}
}```
Example:(TBD)
function treeHeight(node)
{
if (node == null)
return 0;
else
{
/* compute the height of each subtree */
var lheight = treeHeight(node.left);
var rheight = treeHeight(node.right);
/* use the larger one */
if (lheight > rheight)
{
return(lheight + 1);
}
else {
return(rheight + 1);
}
}
}
const chai = require("chai");
const assert = chai.assert
a = new Node("A")
var b = new Node("B");
var c = new Node("C");
var d = new Node("D");
var e = new Node("E");
var f = new Node("F");
var g = new Node("G");
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.left = f;
c.right = g;
;
chai.config.truncateThreshold = 0;
describe("treeHeight", function() {
it("given null", function() {
assert.equal(treeHeight(null), 0);
});
it("given a", function() {
assert.equal(treeHeight(a), 3);
});
it("given b", function() {
assert.equal(treeHeight(b), 2);
});
it("given c", function() {
assert.equal(treeHeight(c), 2);
});
it("given d", function() {
assert.equal(treeHeight(d), 1);
});
it("given e", function() {
assert.equal(treeHeight(e), 1);
});
it("given f", function() {
assert.equal(treeHeight(f), 1);
});
it("given g", function() {
assert.equal(treeHeight(g), 1);
});
// TODO: non-existing value
});