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

最新文章

  1. 使用ADO.NET执行SQL脚本
  2. [BZOJ2599][Race][IOI2011]点分治
  3. oracle中查询某张表都被哪些表参照了
  4. POJ-3162 Walking Race (求树上两点之间最大距离)
  5. [LeetCode]题解(python):155-Min Stack
  6. vim各种编码设置问题
  7. PTA编译总结求最大值及其下标
  8. java29
  9. Linux常用基本命令:三剑客命令之-awk模式用法(1)
  10. Linux网路查看工具
  11. e565. 关闭的时候隐藏窗口
  12. 从Maven仓库中导出jar包
  13. Kali连接不上ssh
  14. VMTurbo采用红帽企业虚拟化软件
  15. SQL Server outer apply 和 cross apply
  16. Linux ps 进程状态码
  17. 《Go语言实战》笔记之第三章 ----包
  18. MySQL定义异常和异常处理方法
  19. LibreOJ 6278 数列分块入门 2(分块)
  20. Jmeter_远程启动

热门文章

  1. 智能指针原理及实现(2)unique_ptr
  2. Phoenix安装批次提交插入更新语句
  3. 跟着我一步一步的搭建一个基于springcloud的微服务实例
  4. Istio调用链埋点原理剖析—是否真的“零修改”分享实录(下)
  5. Tensorflow目录
  6. MySQL--------SQL优化审核工具实战
  7. string::capacity string::size string::length string::max_size
  8. lnmp配置
  9. MyBatis执行原理图
  10. ACM-ICPC 2017 南宁赛区现场赛 M. The Maximum Unreachable Node Set(二分图)