Java Concise Solution, similar to LC124


  • 0
    public class Solution {
        private int res = 0;
        public int diameterOfBinaryTree(TreeNode root) {
            if (root == null) return 0;
            helper(root);
            return res;
        }
        private int helper(TreeNode root) {
            if (root == null) return 0;
            int left = helper(root.left);
            int right = helper(root.right);
            res = Math.max(res, 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.