最深叶节点的公共祖先

/**
 * 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) {}
 * };
 */
#define mmax(a,b)  ((a) >= (b) ? (a) :(b))
int getDepth(struct TreeNode* root){
    if(!root)return 0;

    return mmax(getDepth(root->left),getDepth(root->right))+1;
}
class Solution {
public:
    TreeNode* lcaDeepestLeaves(TreeNode* root) {
        TreeNode *cur = root;
        TreeNode *ret = root;
        while(cur) {
            int  left = getDepth(cur->left);
            int  right = getDepth(cur->right);
            cout < right) {
                cur = cur->left;
            } else {
                cur = cur->right;
            }
        }
        return ret;
    }
};

你可能感兴趣的:(算法)