Frage im Vorstellungsgespräch bei Google

Binary tree: Given any node, write a function dump with returns String 1) returns " " if node is null 2) returns the data if node is a leaf node. 3) returns data, leftChild.data and rightChild.data if node has 2 children

Antwort im Vorstellungsgespräch

Anonym

11. Mai 2016

public static String returnString(TreeNode node) { if(node == null) { return ""; } if(node.left == null && node.right == null) { return node.data; } String leftData = returnString(node.left); String rightData = returnString(node.right); return (root.data + leftData + rightData); }