-
Code treeHeight=$=_=>_?Math.max($(_.left),$(_.right))+1:0
Preloaded Code class Node { constructor(val) { this.data = val; this.left = null; this.right = null; } }
Test Cases 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 });
Output:
-
Code treeHeight=$=_=>!_?0:Math.max($(_.left),$(_.right))+1- treeHeight=$=_=>_?Math.max($(_.left),$(_.right))+1:0
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}