【leetcode】【medium】236. Lowest Common Ancestor of a Binary Tree

236. Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Given the following binary tree:  root = [3,5,1,6,2,0,8,null,null,7,4]

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

Note:

  • All of the nodes' values will be unique.
  • p and q are different and both values will exist in the binary tree.

题目链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/

思路

两种方法都可以用递归和非递归实现,这里只做了递归方法。

扫描二维码关注公众号,回复: 9440605 查看本文章

法一:找路径,再找两个链的最低共同点

先找到两个节点的路径,然后两条路径都从根节点开始,向下找第一个分叉点。

/**
 * 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 || !p || !q) return NULL;
        auto pp = path(root, p);
        auto pq = path(root, q);
        TreeNode* res;
        while(!pp.empty() && !pq.empty() && pp.top() == pq.top()){
            res = pp.top();
            pp.pop();
            pq.pop();
        }
        return res;
    }
    stack<TreeNode*> path(TreeNode* root, TreeNode* p ){
        stack<TreeNode*> res;
        if(!root) return res;
        if(root==p){
            res.push(root);
            return res;
        }else{
            auto l = path(root->left, p);
            auto r = path(root->right, p);
            if(l.size()>0){
                l.push(root);
                return l;
            }
            else if(r.size()>0){
                r.push(root);
                return r;
            }
            else return res;
        }
        
    }
};

法二:标帜

找第一个满足子树上有两个节点的根节点。

/**
 * 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* res = NULL;
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(!root || !p || !q) return NULL;
        find(root, p, q);
        return res;
    }
    bool find(TreeNode* root, TreeNode* p, TreeNode* q){
        if(!root) return NULL;
        int l = (find(root->left, p, q))?1:0;
        int r = (find(root->right, p, q))?1:0;
        int m = (root==p || root==q)?1:0;
        cout<<root->val<<":"<<l<<","<<r<<","<<m<<endl;
        if(l+r+m>=2){
            res = root;
            cout<<root->val;
        }
        return (l+r+m)>0;
    }
};
发布了126 篇原创文章 · 获赞 2 · 访问量 3728

猜你喜欢

转载自blog.csdn.net/lemonade13/article/details/104481898