linode code example
Example 1: lintcode
class BST { constructor() { this.root = null; } addNode(val) { function dfs(node) { if(node === null) { return new TreeNode(val); } if(val >= node.val) { node.right = dfs(node.right); } else { node.left = dfs(node.left); } return node; } this.root = dfs(this.root); }addNodes(arr) { let tree = this; arr.forEach(val => { tree.addNode(val); }) }}
Example 2: lintcode
function height(root) { function dfs(node) { if(node === null) return 0; return Math.max(dfs(node.left), dfs(node.right)) + 1; } dfs(root);}
Example 3: lintcode
function countNodes(root) { function dfs(node) { if(node === null) return 0; return dfs(node.left) + dfs(node.right) + 1; } dfs(root);}
Example 4: lintcode
let arr = [1, 2, 3, 4, 5];for(let i = 0; i < arr.length; i++){ console.log(arr[i]);}