combine leetcode104--the max depth of binary tree


  • 0
    W
        public int diameterOfBinaryTree(TreeNode root) {
            if(root == null) return 0;
            int lhigh = high(root.left);
            int rhigh = high(root.right);
            int ldiameter = diameterOfBinaryTree(root.left);
            int rdiameter = diameterOfBinaryTree(root.right);
            return Math.max(lhigh+rhigh, Math.max(ldiameter, rdiameter));
        }
        
        
        //leetcode104
        public int high(TreeNode root) {
            if(root == null) return 0;
            int left = high(root.left);
            int right = high(root.right);
            if(left > right) {
                return left+1;
            } else {
                return right+1;
            }
        }

Log in to reply
 

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