LeetCode 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.

题目思路

代码 C++

/**
 * 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:
    bool findPath(TreeNode* root, TreeNode* t, vector<TreeNode* >& v){
        bool flag = false;
        
        // 在节点中加入节点
        v.push_back(root);
        // 如果找到节点,则flag=true,不再往下递归,开始返回
        if(root == t){
            flag = true;
        }
        
        // 左孩子不为NULL,且目前还没找到t,才能通过左子树递归
        if(root->left!=NULL && flag==false){
            flag = findPath(root->left, t, v);
        }
        // 右孩子不为NULL,且已经查完左子树,在左子树中没找到t,
        // 才能沿着右子树递归查找,否则在左子树中找到t,则准备返回
        if(root->right!=NULL && flag==false){
            flag = findPath(root->right, t, v);
        }
        
        // 千万注意这一点,在找到节点t之后,不能再出数组了,
        // 此时数组中保存的是根节点到t的路径(如果没有if则路径依次出栈了,程序出错)
        if(flag == false){
            v.pop_back();
        }
        
        return flag;
    }
    
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        // 如果三个节点有一个NULL,则返回NULL
        if(root == NULL || p == NULL || q == NULL){
            return NULL;
        }
        
        vector<TreeNode* > v1;
        vector<TreeNode* > v2;
        // 第一步、找到根节点到两个节点路径
        bool t1 = findPath(root, p, v1);
        bool t2 = findPath(root, q, v2);
        // t1 t2 都为 true 正面在树中找到 p q 
        if(t1==true && t2==true){
            int i=0, j=0;
            TreeNode* tt;
            // 逐步向后比较,找到最后一个相同的节点,即为输入两个节点的最低公共祖先
            while(i < v1.size() && j < v2.size() && v1[i]==v2[j]){
                tt = v1[i];
                i++;
                j++;  
            }
            return tt;
        }
        else{ // 在树中不存在 p q 
            return NULL;
        }
    }
};

总结展望

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,151评论 0 10
  • 文/罗恺 沙漠中的爱 非诚勿扰 办了一千零一期 多少爱依旧在 无垠的荒漠中哭泣 见过世面的积雨云 都遗忘了同情和怜...
    罗恺阅读 5,039评论 36 47
  • 那一刻我知道,我把他平安、成功地带来到这个世界,他用尽全力终于完成了人生的第一步,与我这个妈妈在肉体上分开,成为了...
    林秋千阅读 14,365评论 4 4
  • 柿原彻也是在2000年代出道的声优,可以讲一口流利的德语,在第一届声优奖中获得新人男声优奖。他在今年就为《妖精的尾...
    二次元冒险团阅读 11,703评论 0 0
  • 从小到大都不知为好,何为坏,只是单纯的认为我对你好,就算你现在对我不怎么样,但是以后肯定对我好的,我习惯性的...
    浅浅的鱼丿阅读 1,371评论 0 0