C++算法基础之树篇(下)

上篇博客中介绍了基本的树在模板下的前中后序遍历,这篇主要是将树的递归的应用,其主要的方法时用递归来做。
1.树的子结构

bool isTree(TreeNode* A, TreeNode* B) {  //这个函数判断A与B是否严格相同或者B为子树,如果B为子树一定先为NULL
    if (A == NULL) return false;
    if (B == NULL) return true;
    if (A->val != B->val) return false;

    return isTree(A->left, B->left) && isTree(A->right, B->right);
}

bool isSubStructure(TreeNode* A, TreeNode* B) {
    if (A == NULL || B == NULL) return false;

    return isTree(A, B) || isSubStructure(A->left, B) || isSubStructure(A->right, B);
}

2.树的镜像

//递归
TreeNode* mirrorTree(TreeNode* root) {
    
    if (root == NULL) return NULL;
    TreeNode* temp = root->right;
    root->right = root->left;
    root->left = temp;

    if (root->left) mirrorTree(root->left);
    if (root->right) mirrorTree(root->right);
    return root;
}
//迭代栈
TreeNode* mirrorTree_stack(TreeNode* root) {
    if (root == NULL) return NULL;
    stack<TreeNode*> s;
    s.push(root);
    while (!s.empty()) {
        TreeNode* temp = s.top();
        s.pop();
        if (temp->left) s.push(temp->left);
        if (temp->right) s.push(temp->right);
        swap(temp->left, temp->right);
    }
    return root;
}

3.对称二叉树

bool compare(TreeNode* left, TreeNode* right) {
    if (left == NULL && right != NULL) return false;
    else if (left != NULL && right == NULL) return false;
    else if (left == NULL && right == NULL) return true;
    else if (left->val != right->val) return false;
    
    return compare(left->left, right->right) && compare(left->right, right->left);
}
bool isSymmetric(TreeNode* root) {
    if (root == NULL) return true;
    
    return compare(root->left, root->right);
    
}

4.从上到下打印二叉树 Z字打印

//双栈
vector<vector<int>> levelOrder(TreeNode* root) {
    
    vector<vector<int>> res;
    if (root == NULL) return res;
    stack<TreeNode*> s;
    stack<TreeNode*> k;
    s.push(root);
    int level = 1;
    while (!s.empty()||!k.empty()) {
        vector<int> Subres;
        if (level % 2 == 1) {
            while (!s.empty()) {
                TreeNode* cur = s.top();
                s.pop();
                if (cur->left) k.push(cur->left);
                if (cur->right) k.push(cur->right);
                Subres.push_back(cur->val);
            }
            
        }
        else {
            while (!k.empty()) {
                TreeNode* cur = k.top();
                k.pop();
                if (cur->right) s.push(cur->right);
                if (cur->left) s.push(cur->left);
                Subres.push_back(cur->val);
            }

        }
        res.push_back(Subres);
        ++level;
    }
    return res;
}

5.二叉树的深度

int maxdepth(TreeNode* root) {
    if (root == NULL) return 0;
    return max(maxdepth(root->left), maxdepth(root->right)) + 1;
}

bool isBalanced(TreeNode* root) {
    if (root == NULL) return false;
    bool flag = abs(maxdepth(root->left)- maxdepth(root->right)<=1);

    return flag && isBalanced(root->left) && isBalanced(root->right);
}

6.判断平衡二叉树

int maxdepth(TreeNode* root) {
    if (root == NULL) return 0;
    return max(maxdepth(root->left), maxdepth(root->right)) + 1;
}

bool isBalanced(TreeNode* root) {
    if (root == NULL) return false;
    bool flag = abs(maxdepth(root->left)- maxdepth(root->right)<=1);

    return flag && isBalanced(root->left) && isBalanced(root->right);
}

6.二叉搜索树的最近公共祖先

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    if (root == NULL) return root;
    TreeNode* res = root;
    while (true) {
        if ((p->val < res->val) && (q->val < res->val)) {
            res = res->left;
        }
        else if ((p->val > res->val) && (q->val > res->val)) {
            res = res->right;
        }
        else break;
    }
    return res;
}

7.二叉树的公共祖先

TreeNode* lowestCommonAncestor_1(TreeNode* root, TreeNode* p, TreeNode* q) {
    if (!root || !q || !p || q == root || p == root) return root;
    TreeNode* leftTree = lowestCommonAncestor_1(root->left, p, q);
    TreeNode* rightTree = lowestCommonAncestor_1(root->right, p, q);

    if (!leftTree && !rightTree) return NULL;
    else if (leftTree && (!rightTree)) return leftTree;
    else if (leftTree && rightTree) return root;
    else return rightTree;

}

8.二叉树中和为某一值的路径 34 ****void 这个题有点意思 cur 不需要重新也能 target 不能传入地址

void Path(vector<vector<int>>& res, vector<int>& cur, TreeNode* root, int& target) {
    if (root == NULL) return;
    target -= root->val;
    cur.push_back(root->val);
    if (target == 0&&root->left==NULL&&root->right==NULL) {
        res.push_back(cur);
    }
    Path(res, cur, root->left, target);
    Path(res, cur, root->right, target);
    cur.pop_back();
}

vector<vector<int>> pathSum(TreeNode* root, int target) {
    vector<vector<int>> res;
    vector<int> cur;
    Path(res, cur, root, target);

    return res;
    

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值