simple JAVA Recursive solution.


  • 0
    S
    public class Solution {
        private int max;
        public int diameterOfBinaryTree(TreeNode root) {
            helper(root);
            return max;
        }
        private int helper(TreeNode root) {
            if (root == null) {
                return -1;
            }
            int left = helper(root.left);//single longest path go through roo.left;
            int right = helper(root.right);
            max = Math.max(max, 2 + left + right);
            return 1 + Math.max(left, right);
        }
    }
    

Log in to reply
 

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