zoukankan      html  css  js  c++  java
  • LC 971. Flip Binary Tree To Match Preorder Traversal

    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]);
        }
        }
    };
  • 相关阅读:
    C# 设计模式之简单工厂模式
    CentOS7下二进制文件安装MySQL5.6
    CentOS7下源码安装5.6.23
    CentOS7下yum方式安装mysql5.6
    关于网页图标使用与字体图标制作
    【canvas系列】canvas实现“ 简单的Amaziograph效果”--画对称图【强迫症福利】
    【canvas系列】canvas实现"雷达扫描"效果
    【canvas系列】用canvas实现一个colorpicker(类似PS的颜色选择器)
    如何在vue自定义组件中使用v-model
    webpack4 splitChunksPlugin && runtimeChunkPlugin 配置杂记
  • 原文地址:https://www.cnblogs.com/ethanhong/p/10301723.html
Copyright © 2011-2022 走看看