C++ 5 lines DFS


  • 0
        int diameterOfBinaryTree(TreeNode* root) {
            if(!root) return 0;
            int len=DFS(root->left)+DFS(root->right);
            return max(len,max(diameterOfBinaryTree(root->left),diameterOfBinaryTree(root->right)));
        }
        
        int DFS(TreeNode* root){
            if(!root) return 0;
            return max(DFS(root->left)+1,DFS(root->right)+1);
        }
    

Log in to reply
 

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