原题链接:Leecode 1123. 最深叶节点的最近公共祖先
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int height(TreeNode* root)
{
if(!root) return 0;
return max(height(root->left),height(root->right))+1;
}
TreeNode* lcaDeepestLeaves(TreeNode* root) {
if(!root) return root;
int l=height(root->left);
int r=height(root->right);
if(l==r) return root;
if(l>r) return lcaDeepestLeaves(root->left);
return lcaDeepestLeaves(root->right);
}
};