Straightforward Java solution


  • 0
    public class Solution {
        public int diameterOfBinaryTree(TreeNode root) {
            helper(root);
            return diameter;
        }
        
        int diameter = 0;
        private int helper(TreeNode node) {
            if (node == null)   return 0;
            
            int left = helper(node.left);
            int right = helper(node.right);
            
            diameter = Math.max(diameter, left + right);
            return Math.max(left, right) + 1;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.