最近公共祖先

来源:互联网 发布:精功科技收购盘古数据 编辑:程序博客网 时间:2024/05/30 05:18
//代码如下class Solution {public:    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {        if(root == NULL)            return NULL;        if(root == p || root == q)            return root;        TreeNode * left = lowestCommonAncestor(root->left, p, q);        TreeNode * right = lowestCommonAncestor(root->right, p, q);        if(left && right)            return root;        if(!left && !right) //这一句实际上和下一句可以合成为一句            return NULL;        return left ? left : right;    }};
0 0
原创粉丝点击