[LeetCode 解题报告]236. Lowest Common Ancestor of a Binary Tree

我的未来我决定 提交于 2020-02-08 18:20:52

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.

Method 1. 

/**
 * 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 || q == root)
            return root;
        
        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);
        
        if (left && right)
            return root;
        return left ? left : right;
    }
};

Method 2. 

/**
 * 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;
        
        stack<TreeNode*> s{{root}};
        vector<TreeNode*> v;
        bool tag1 = false, tag2 = false;
        TreeNode* lastRoot = root;
        
        while (!s.empty()) {
            root = s.top();
            if (root == p) {
                if (tag1 == false && tag2 == false)
                    v.push_back(root);
                tag1 = true;
            }
            else if (root == q) {
                if (tag1 == false && tag2 == false)
                    v.push_back(root);
                tag2 = true;
            }
            
            if (!tag1 && !tag2)
                v.push_back(root);
            
            if (tag1 && tag2 && find(v.begin(), v.end(), root) != v.end())
                return root;
            
            if (lastRoot != root->right) {
                if (lastRoot != root->left) {
                    if (root->left != NULL) {
                        s.push(root->left);
                        continue;
                    }
                }
                
                if (root->right != NULL) {
                    s.push(root->right);
                    continue;
                }
            }
            
            lastRoot = root;
            s.pop();
        }
        return NULL;
    }
};

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!