Pergunta de entrevista da empresa Amazon

Write method to count nodes on a binary tree with integer nodes that match a given values

Respostas da entrevista

Sigiloso

19 de jan. de 2016

public int countNodes(Node node, int value) { if(node == null) return 0; int count = value == (Integer) node.data ? 1 : 0; if(node.left != null) count += countNodes(node.left, value); if(node.right != null) count += countNodes(node.right, value); return count; }

1

Sigiloso

6 de ago. de 2015

A Method to Count the Number of Leaf Nodes in a Binary Tree public int leafCount( Node root ) { if ( root == null ) return 0; else if ( (root.left != null) || (root.right != null) ) return (leafCount(root.left) + leafCount(root.right)); else return 1; //if we got here this must be a leaf }