Tree: Height of a Binary Tree
Tree: Height of a Binary Tree
+ 0 comments The code provided for Javascript is broken.
You can copy this wrapper and use typescript. Type your solution inside the treeHeight function.
'use strict'; process.stdin.resume(); process.stdin.setEncoding('utf-8'); // process.stdin.setEncoding('ascii'); let inputString: string = ''; let inputLines: string[] = []; let currentLine: number = 0; process.stdin.on('data', function(inputStdin: string): void { inputString += inputStdin; }); process.stdin.on('end', function(): void { inputLines = inputString.split(/\s/); inputString = ''; solution(); }); function readLine(): string { return inputLines[currentLine++]; } class TreeNode { data:number; left:null|TreeNode; right:null|TreeNode; constructor(data:number){ this.data = data; this.left = null; this.right = null; } } class Tree { root:TreeNode|null; constructor() { this.root = null; } insert(node:TreeNode, data:number) { if (node == null){ node = new TreeNode(data); } else if (data < node.data){ node.left = this.insert(node.left, data); } else{ node.right = this.insert(node.right, data); } return node; } } // This is a "method-only" submission. // You only need to complete this method. function treeHeight(root:TreeNode):string { return "Your solution in here"; } function solution() { var tree = new Tree(); var n = parseInt(readLine()); for (var i=0; i<n; i++) { var m = parseInt(readLine()); tree.root = tree.insert(tree.root, m); } var height = treeHeight(tree.root); process.stdout.write(height); }
+ 0 comments Python 3, breadth-first traversal, without recursion
def height(root): nodes = {0 : [root]} h = 0 while nodes.get(h): nodes[h+1] = [] for node in nodes[h]: if node.left: nodes[h+1].append(node.left) if node.right: nodes[h+1].append(node.right) h += 1 return max(nodes.keys()) - 1
+ 0 comments simple c++ recursive solution
int height(Node* root) { if(root==NULL){return -1;} else{ int left = height(root->left); int right = height(root->right); return max(left,right)+1; } }
+ 1 comment (JavaScript) Bug report on building a tree. For the example:
7 3 5 2 1 4 6 7
It creates an
_stdin_array = [ '7', '3 5 2 1 4 6 7' ]
but the methodreadLine()
tries to readn
lines/elements after reding the first line(_stdin_array[0]
) to assign the value ton
but we only have one more line/element. Thus, the first node is created correctly becauseparseInt('3 5 2 1 4 6 7' );
but later tries to get_stdin_array[2]
and_stdin_array[3]
and so on, elements that not exists in the array and it gets anundefined
andparseInt(undefined)
returns aNaN
. You end up with a right-loaded tree with mostlyNaN
data values.
+ 0 comments In Swift
code gave run time because boilerplate code. Hackerrank must fix this line: " root = tree.insert(root: root, data: Int(readLine()!)!) ".
func getHeight(root: Node?) -> Int { if root == nil { return -1 } else { let left = getHeight(root: root?.left) let right = getHeight(root: root?.right) if left > right { return left + 1 } else { return right + 1 } } }
Sort 864 Discussions, By:
Please Login in order to post a comment