LC 971. Flip Binary Tree To Match Preorder Traversal

本文介绍了一种算法,旨在通过最少次数的节点翻转使二叉树的前序遍历与给定序列相匹配。文章提供了一个C++实现方案,该方案在在线提交中运行速度超过99.80%,并详细解释了递归辅助函数的逻辑,用于判断是否可以成功翻转,并记录翻转的节点。

Given a binary tree with N nodes, each node has a different value from {1, ..., N}.

A node in this binary tree can be flipped by swapping the left child and the right child of that node.

Consider the sequence of N values reported by a preorder traversal starting from the root.  Call such a sequence of N values the voyage of the tree.

(Recall that a preorder traversal of a node means we report the current node's value, then preorder-traverse the left child, then preorder-traverse the right child.)

Our goal is to flip the least number of nodes in the tree so that the voyage of the tree matches the voyage we are given.

If we can do so, then return a list of the values of all nodes flipped.  You may return the answer in any order.

If we cannot do so, then return the list [-1].

 

 

Runtime: 4 ms, faster than 99.80% of C++ online submissions for Flip Binary Tree To Match Preorder Traversal.

 

 

//
// Created by yuxi on 2019-01-18.
//

#include <vector>
#include <unordered_map>
using namespace std;


/**
 * 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 {
private:
  unordered_map<int,int> mp;
public:
  vector<int> flipMatchVoyage(TreeNode* root, vector<int>& voyage) {
    for(int i=0; i<voyage.size(); i++) mp[voyage[i]] = i;
    vector<int> ret;
    bool flag = helper(ret, root, 0);
    if(!flag) return {-1};
    return ret;
  }
  bool helper(vector<int>& ret, TreeNode* root, int idx){
    if(!root) return true;
    if(mp[root->val] != idx) return false;
    int left = root->left ? mp[root->left->val] : -1;
    int right = root->right ? mp[root->right->val] : -1;
    if((left >= 0 && left < mp[root->val]) || (right >= 0 && right < mp[root->val])) return false;
    if(left == -1 && right == -1) return true;
    else if(left == -1 && right != -1) {
      if(right != idx+1) return false;
      return helper(ret, root->right, mp[root->right->val]);
    }
    else if(left != -1 && right == -1) {
      if(left != idx+1) return false;
      return helper(ret, root->left, mp[root->left->val]);
    }
    else if(left > right) {
      if(right != idx+1) return false;
      ret.push_back(root->val);
      return helper(ret, root->right, idx+1) && helper(ret, root->left, mp[root->left->val]);
    }
    else {
      if(left != idx+1) return false;
      return helper(ret, root->left, idx+1) && helper(ret, root->right, mp[root->right->val]);
    }
    }
};

 

转载于:https://www.cnblogs.com/ethanhong/p/10301723.html

### OpenCV中`cv2.flip`函数的使用说明 #### 函数定义 `cv2.flip()` 是 OpenCV 提供的一个用于图像翻转的功能函数。其基本语法如下: ```python dst = cv.flip(src, flipCode[, dst]) ``` 其中: - `src`: 输入图像。 - `flipCode`: 指定翻转方向的参数,可以取值为 0、大于 0 或小于 0 的整数[^1]。 - 当 `flipCode=0` 时,表示围绕 X 轴进行翻转(垂直翻转)。 - 当 `flipCode>0` 时,表示围绕 Y 轴进行翻转(水平翻转)。 - 当 `flipCode<0` 时,表示同时围绕 X 和 Y 轴进行翻转(即先水平再垂直翻转)。 返回值 `dst` 表示经过翻转后的输出图像。 --- #### 示例代码展示 以下是基于 Python 编写的 `cv2.flip()` 使用案例,演示如何实现不同类型的图像翻转操作: ```python import cv2 # 加载原始图像 img = cv2.imread('example.jpg') if img is None: print("Error: Image not found.") else: # 垂直翻转 (X轴) vertical_flip = cv2.flip(img, 0) # 水平翻转 (Y轴) horizontal_flip = cv2.flip(img, 1) # 同时水平和垂直翻转 (XY轴) both_flip = cv2.flip(img, -1) # 显示原图与翻转结果 cv2.imshow('Original', img) cv2.imshow('Vertical Flip', vertical_flip) cv2.imshow('Horizontal Flip', horizontal_flip) cv2.imshow('Both Flip', both_flip) # 等待按键关闭窗口 cv2.waitKey(0) cv2.destroyAllWindows() ``` 上述代码加载了一张名为 `'example.jpg'` 的图片文件,并分别执行了三种不同的翻转方式:仅垂直翻转、仅水平翻转以及两者结合的双轴翻转[^4]。 --- #### 应用场景举例 该函数广泛应用于计算机视觉领域中的数据增强技术,在训练机器学习模型之前通过对样本集内的每一张图片应用随机翻转变换来扩充数据量,从而提高模型泛化能力[^2]。 另外值得注意的是,除了简单的几何变换外,还可以配合其他 OpenCV 功能模块完成更复杂的任务,比如在二值化处理前调整目标物体的方向以便后续分割提取更加精确[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值