Skip to content

Instantly share code, notes, and snippets.

@bastienapp
Created July 19, 2019 09:57
Show Gist options
  • Select an option

  • Save bastienapp/934203319f8bf57415e4c10ca72a1fbc to your computer and use it in GitHub Desktop.

Select an option

Save bastienapp/934203319f8bf57415e4c10ca72a1fbc to your computer and use it in GitHub Desktop.
import java.util.Map;
public class FindMax {
/*
13
\
5
/ \
16 17
13
compare à droite : max(5) == 17
5
compare à droite : max(17) == 17
value : 17
compare à gauche: max(16) == 16
17
compare à droite : max(null)
compare à gauche : max(null)
*/
public static int max(TreeNode root) {
if (root == null) {
return 0;
}
int value = root.getValue();
value = Math.max(value, max(root.getRight()));
value = Math.max(value, max(root.getLeft()));
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment