LeetCode 971. Flip Binary Tree To Match Preorder Traversal
原题链接在这里:https://leetcode.com/problems/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].
Example 1:

Input: root = [1,2], voyage = [2,1]
Output: [-1]
Example 2:

Input: root = [1,2,3], voyage = [1,3,2]
Output: [1]
Example 3:

Input: root = [1,2,3], voyage = [1,2,3]
Output: []
Note:
1 <= N <= 100
题解:
Use DFS to check if current subtree is eligible to flip.
If current root's val != voyage[i], then it is not eligible.
Otherwise, i++. And if left child's val != voyage[i], flip, add root.val to res. Check right subtree first, then left.
If the whole tree is not eligible to flip, return -1.
Time Complexity: O(n).
Space: O(h).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<Integer> res = new ArrayList<Integer>();
int i = 0; public List<Integer> flipMatchVoyage(TreeNode root, int[] voyage) {
return dfs(root, voyage) ? res : Arrays.asList(-1);
} private boolean dfs(TreeNode root, int[] voyage){
if(root == null){
return true;
} if(root.val != voyage[i]){
return false;
} i++;
if(root.left != null && root.left.val != voyage[i]){
res.add(root.val);
return dfs(root.right, voyage) && dfs(root.left, voyage);
} return dfs(root.left, voyage) && dfs(root.right, voyage);
}
}
Need practice Again.
LeetCode 971. Flip Binary Tree To Match Preorder Traversal的更多相关文章
- 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 b ...
- 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...
- [Swift]LeetCode971.翻转二叉树以匹配先序遍历 | 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 b ...
- 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
// 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...
- 【LeetCode】103. Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...
- LeetCode OJ:Binary Tree Zigzag Level Order Traversal(折叠二叉树遍历)
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
随机推荐
- Linux nfs服务讲解
nfs服务介绍 nfs(Network File System) 网络文件系统,能使用户访问服务器的文件系统,就像访问自己的本机的文件系统一样,并且多个 客户端共享访问该文件系统. 目前nfs服务,较 ...
- 创建包含CRUD操作的Web API接口5:实现Delete方法
本节是前面四节的延续,在前面几节中我们创建了Web API并添加了必要的基础设施,实现了Get.Post.和Put方法.本节中,我们将介绍如何在Web API中实现Delete方法. 在RESTful ...
- 论文笔记 : NCF( Neural Collaborative Filtering)
ABSTRACT 主要点为用MLP来替换传统CF算法中的内积操作来表示用户和物品之间的交互关系. INTRODUCTION NeuCF设计了一个基于神经网络结构的CF模型.文章使用的数据为隐式数据,想 ...
- WPF 程序的编译过程
原文:WPF 程序的编译过程 基于 Sdk 的项目进行编译的时候,会使用 Sdk 中附带的 props 文件和 targets 文件对项目进行编译.Microsoft.NET.Sdk.WindowsD ...
- python中format函数用于字符串的格式化
python中format函数用于字符串的格式化 通过关键字 print('{名字}今天{动作}'.format(名字='陈某某',动作='拍视频'))#通过关键字 grade = {'name' : ...
- MyCat - 数据库中间插件
什么是MyCat 是目前最流行的分布式数据库中间插件 为什么使用MyCat 如今随着互联网的发展,数据的量级也是撑指数的增长,从GB到TB到PB.对数据的各种操作也是愈加的困难,传统的关系性数据库已经 ...
- ps 修补工具
最近刚好遇到需要p图去除水印,这里将ps去除水印的使用记录下来已备翻阅 1.需求图片(如下),使用软件 photo shop cc 2017(以下简称ps) 2.操作 2.1方法一 使用五点修复画笔工 ...
- 钻石diamaund外语
Whenever I fail as a father or husband... a toy and a diamond always works. I never worry about diet ...
- ubuntu16.04安装openssh中报错解决
在使用apt-get直接进行安装时会报错: sudo apt-get install openssh-server 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态 ...
- SpringBoot上传文件报错,临时路径不存在
异常信息 报错日志: The temporary upload location [/tmp/tomcat.7957874575370093230.8088/work/Tomcat/localhost ...