Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- int max = 0;
- public int diameterOfBinaryTree(TreeNode root) {
- maxDepth(root);
- return max;
- }
- public int maxDepth(TreeNode root){
- if(root == null) return 0;
- int left = maxDepth(root.left);
- int right = maxDepth(root.right);
- max = Math.max(max, left + right);
- return Math.max(left, right) + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement