本文旨在讲解有关二叉树的OJ题目,希望读完本文,能让读者都二叉树有更深一步的认识!
正文开始!
106. 根据二叉树创建字符串

算法思想:
根据题目的输出结果,可以观察出如下规律!
1.若左右结点都为空时,括号可以进行省略!
2.当右结点为空时,括号可以省略!
3.当左结点为空时,括号不可以省略!
根据此规律可以写出如下代码:
代码如下:
* Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    //当左为空的时候括号不能省略!
    //右为空的话,括号可以省略!
    //左右都为空的时候可以省略!
    string tree2str(TreeNode* root) 
    {
        
        string str;
        if(root==nullptr)
        {
            return str;
        }
        str+=to_string(root->val);
        if(root->left||root->right)
        {
            str+='(';
            str+=tree2str(root->left);
            str+=')';
        }
        if(root->right)
        {
            str+='(';
            str+=tree2str(root->right);
            str+=')';
        }
        return str;
        
    }
    
};代码讲解:
首先先创建一个string变量,用于记录结果!当根结点为空时,直接返回str即可!本代码利用了递归的思想进行了求解!递归将一个大问题转化为若干个小问题进行解决,首先需要有递归解决子问题的算法,以及递归的结束条件!
我们可以将一个整个结构分解成若干个根结点和其左右结点的子问题进行求解!!再根据算法思想即可将代码补充完毕!
236. 二叉树的最近公共祖先


法一:根据特性进行求解!观察实例可以得出以下规律!
1.若p,q其中两节点有一者与root相等,那么当前oot就是他们最近的公共祖先!
2.当p,q处于当前root的一左一右的时候,此时的root也是他们最近公共祖先!
又因为题目说一定存在公共祖先,所以当二者位于同一侧的时候,直接遍历到此侧的子树中进行求解即可!
算法思想:
根据特性进行算法分析!
代码如下:
/**
 * 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:
    //根据特性进行求解!
    // 1.两个结点中其中一个结点为root结点,即返回root即可!
    // 2.当两个结点分布在当前root的一左一右时,当前root即为所求!
    //此方法时间复杂度为O(n2)!
    bool istree(TreeNode*root,TreeNode*x)
    {   
        if(root==nullptr)
        {
            return false;
        }
        if(root==x)
        {
            return true;
        }
        
        return istree(root->left,x)||istree(root->right,x);
    }
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) 
    {
        //特性1!
        if(p==root||q==root)
        {
            return root;
        }
        bool pisleft=istree(root->left,p);
        bool pisright=!pisleft;
        bool qisleft=istree(root->left,q);
        bool qisright=!qisleft;
        //此时p,q的位置已经确定!!
        //一个在左一个在右!
        if((pisleft&&qisright)
        ||(qisleft&&pisright))
        {
            return root;
        }
        //都在左
        if(pisleft&&qisleft)
        {
            return lowestCommonAncestor(root->left,p,q);
        }
        if(pisright&&qisright)
        {
            return lowestCommonAncestor(root->right,p,q);
        } 
        return nullptr;
    }
};但是此时的时间复杂度是n方级别的!那么是否有更简单的方法呢?
法二:利用路径的思路进行求解!
算法思想:
求出两个结点的路径,然后根据路径进行求出最近公共祖先!
代码如下:
/**
 * 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* x,stack<TreeNode*>& path)
    {
        if(root==nullptr)
        {
            return false;
        }
        path.push(root);
        if(root==x)
        {
            return true;
        }
        if(Findpath(root->left,x,path))
        {
            return true;
        }
        if(Findpath(root->right,x,path))
        {
            return true;
        }
        path.pop();
        return false;
    }
    
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) 
    {
        stack<TreeNode*>ppath;
        stack<TreeNode*>qpath;
        Findpath(root,p,ppath);
        Findpath(root,q,qpath);
        while(ppath.size()!=qpath.size())
        {
            if(ppath.size()>qpath.size())
            {
                ppath.pop();
            }
            else
            {
                qpath.pop();
            }
        }
        while(ppath.top()!=qpath.top())
        {
            ppath.pop();
            qpath.pop();
        }
        return ppath.top();
    }
};


















