zoukankan      html  css  js  c++  java
  • leetcode 236. 二叉树的最近公共祖先

     

    代码:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    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==NULL)
            {
                return right;
            }
            if(right == NULL)
            {
                return left;
            }
            if(left&&right)//P 和q 在两侧
            {
                return root;
            }
            return NULL;
        }
    };
    以大多数人努力程度之低,根本轮不到去拼天赋~
  • 相关阅读:
    第二天续
    使用git提交本地仓库每次需要输入账号密码的问题解决
    第二天
    开启远程之路
    第一天
    第一步了解并且安装配置
    6
    Algorithms
    Algorithms
    Algorithms
  • 原文地址:https://www.cnblogs.com/gcter/p/15338648.html
Copyright © 2011-2022 走看看