原题链接在这里: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. 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的更多相关文章

  1. 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 ...

  2. 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...

  3. [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 ...

  4. 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  5. leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历

    // 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...

  6. 【LeetCode】103. Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  7. 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...

  8. 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 ...

  9. 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

随机推荐

  1. Vim 入门

    Vim 简介 打开 Vim的四种模式 一些命令 插入 移动 文件 编辑 环境设置 .vimrc 更多命令 环境设置 折叠 显示 Vim 简介 Vim 是字符模式下的一种文本编辑器,不需要图形界面,它是 ...

  2. 【Python爬虫案例学习】下载某图片网站的所有图集

    前言 其实很简短就是利用爬虫的第三方库Requests与BeautifulSoup. 其实就几行代码,但希望没有开发基础的人也能一下子看明白,所以大神请绕行. 基本环境配置 python 版本:2.7 ...

  3. DISPLAY FORMAT 語法

  4. 解决html 图片缓存问题

    <!--问题:上传一张图片,通过js更新src属性刷新图片使其即时显示时, 当img的src当前的url与上次地址无变化时(只更改图片,名称不变,不同图片名称相同)图片不变化(仍显示原来的图片) ...

  5. 配置安全web服务

    为站点 http://system1.group8.example.com 配置TLS加密: 1.一个已签名证书从 http://server.group8.example.com/pub/tls/c ...

  6. pytest_01-环境准备与入门

    前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...

  7. CF704D Captain America 上下界网络流

    传送门 现在相当于说每一个条件都有一个染成红色的盾牌的数量限制\([l,r]\),需要满足所有限制且染成红色的盾牌数量最小/最大. 注意到一个盾牌染成红色对于一行和一列都会产生影响.如果选中一个物品对 ...

  8. FastJson前置属性过滤器

    FastJson前置属性过滤器 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p ...

  9. NIO开发Http服务器(5-完结):HttpServer服务器类

    最近学习了Java NIO技术,觉得不能再去写一些Hello World的学习demo了,而且也不想再像学习IO时那样编写一个控制台(或者带界面)聊天室.我们是做WEB开发的,整天围着tomcat.n ...

  10. el-select和el-cascader的visible-change下拉框隐藏时触发相关事件(下拉框下拉显示时不触发)

    原文:https://blog.csdn.net/CarryBest/article/details/79959389 今天做项目时,用elementUI框架,需要下拉框隐藏时出发某个函数,用了vis ...