Created
July 19, 2019 09:57
-
-
Save bastienapp/934203319f8bf57415e4c10ca72a1fbc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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