子树/最近公共祖先

判断一颗二叉树是是否是另一颗树的子树

class Solution {
public:
     bool test(TreeNode* T1,TreeNode* T2)
     {
        if(T1==NULL && T2==NULL)
            return true;
        if(T1==NULL||T2==NULL||T1->val!=T2->val)
            return false;

        return test(T1->left,T2->left)&&test(T1->right,T2->right);
     }
    bool isSubtree(TreeNode *T1, TreeNode *T2) {
        // write your code here
        if( T2==NULL )
            return true;
        if(T1==NULL)
            return false;
        if(T1->val==T2->val)
        {
            if(test(T1,T2))
                return true;
        }
        return isSubtree(T1->left,T2)||isSubtree(T1->right,T2);
    }
};

判断一棵树是否是完全二叉树

bool istree(TreeNode* root)
{
    queue<TreeNode*> q;
    q.push(root);
    int count = 1;
    int isbintree = 1;
    while (!q.empty())
    {
        count = q.size();
        while (count--)
        {

            Node* tmp = q.front();
            q.pop();
            cout << tmp->val << endl;
            if (tmp->left)
            {
                if (isbintree == 0)
                    return false;
                q.push(tmp->left);

            }
            else
                isbintree = 0;
            if (tmp->right)
            {
                if (isbintree == 0)
                    return false;
                q.push(tmp->right);
            }
            else
                isbintree = 0;
        }
    }
    return true;
}

二叉树中两个节点的最近公共祖先

要求考虑以下三种种情况,给出解决方案,并解决:
1:二叉树每个节点有parent(三叉链)
相当于转换成链表求交点问题
2:二叉树是搜索二叉树。
直接找比他们最大还大的节点
3:就是普通二叉树。(尽可能实现时间复杂度为(N ))
通过路径来求

class Solution {
public:
    /**
     * @param root: The root of the binary search tree.
     * @param A and B: two nodes in a Binary.
     * @return: Return the least common ancestor(LCA) of the two nodes.
     */
bool GetNodePath(TreeNode* cur, TreeNode* node, vector<TreeNode*>& l)
{
    if (cur == node)
    {
        l.push_back(cur);
        return true;
    }
    if(cur==NULL)
        return false;
    l.push_back(cur);
    bool found = false;
    if (!found && cur->left)
        found = GetNodePath(cur->left, node, l);
    if (!found && cur->right)
        found = GetNodePath(cur->right, node, l);
    if (!found)
        l.pop_back();
    return found;
}

    TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *A, TreeNode *B) {
        // write your code here
        if(root==A||root==B||root==NULL)
            return root;
        vector<TreeNode*> path1;
        GetNodePath(root,A,path1);
        vector<TreeNode*> path2;
        GetNodePath(root,B,path2);
        int size=path1.size()>path2.size()?path2.size():path1.size();
        int i=0;
        TreeNode* ret=NULL;
        for(;i<size;i++)
        {
            if(path1[i]==path2[i])
                ret=path1[i];
            else
                break;
        }

            return ret;
    }       
};
Java中二叉树最近公共祖先可以通过递归来解决。首先,我们需要定义一个TreeNode类表示二叉树的节点,其中包含值和左右子节点的引用。下面是一个示例代码: ```java class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } } ``` 接下来,我们可以实现一个递归函数来找到最近公共祖先: ```java public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { // 判断当前节点是否为空或者等于p或q if (root == null || root == p || root == q) { return root; } // 在左子树中寻找最近公共祖先 TreeNode left = lowestCommonAncestor(root.left, p, q); // 在右子树中寻找最近公共祖先 TreeNode right = lowestCommonAncestor(root.right, p, q); // 如果左子树和右子树都找到了最近公共祖先,则当前节点就是最近公共祖先 if (left != null && right != null) { return root; } // 如果只有左子树找到了最近公共祖先,则返回左子树的结果 if (left != null) { return left; } // 如果只有右子树找到了最近公共祖先,则返回右子树的结果 if (right != null) { return right; } // 如果左右子树都没有找到最近公共祖先,则返回null return null; } ``` 这个递归函数的基本思路是: - 当前节点为空或者等于p或q时,直接返回当前节点; - 在左子树中寻找最近公共祖先; - 在右子树中寻找最近公共祖先; - 如果左子树和右子树都找到了最近公共祖先,则当前节点就是最近公共祖先; - 如果只有左子树找到了最近公共祖先,则返回左子树的结果; - 如果只有右子树找到了最近公共祖先,则返回右子树的结果; - 如果左右子树都没有找到最近公共祖先,则返回null。 你可以调用这个函数来找到二叉树中任意两个节点的最近公共祖先
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值