Lowest Common Ancestor of Deepest Leaves LeetCode solution in C++
1class Solution {2public:3int getDepth(TreeNode *root) {4if (!root) return 0;5return max(getDepth(root->right), getDepth(root->left)) + 1;6}78TreeNode *lcaDeepestLeaves(TreeNode *root) {9if (!root) return NULL;1011int right_depth = getDepth(root->right);12int left_depth = getDepth(root->left);1314if (right_depth == left_depth) return root;1516if (right_depth > left_depth)17return lcaDeepestLeaves(root->right);1819else20return lcaDeepestLeaves(root->left);21}22};