原题链接在这里: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. Linux下signal信号汇总

    SIGHUP /* Hangup (POSIX). */ 终止进程 终端线路挂断 SIGINT /* Interrupt (ANSI). */ 终止进程 中断进程 Ctrl+C SIGQUIT /* ...

  2. JDK8源码解析 --- Long 类型

    最近都在看JDK8的源码,想把记录下来与大家一起共享,每天 积累一点,每天成长一点.看了装箱Long类型,有好多以前没有注意到或者不知道的内容,慢慢懂得.废话不多说,直接上代码讲解... 1.缓存区L ...

  3. Linux中解压、压缩 ZIP文件

    解压 unzip -o -d /home/v-gazh myfile.zip # 把myfile.zip文件解压到 /home/v-gazh/ # -o:不提示的情况下覆盖文件: # -d:-d /h ...

  4. 【转载】 C#使用Newtonsoft.Json组件来反序列化字符串为对象

    在Asp.Net网站开发的过程中,很多时候会遇到对象的序列化和反序列化操作,Newtonsoft.Json组件是专门用来序列化和反序列化操作的一个功能组件,引入这个DLL组件后,就可使用JsonCon ...

  5. 利用onMouseOver和onMouseOut实现图像翻滚

    代码: <img src="images/001.jpg" alt="pic" onmouseover="this.src='images/00 ...

  6. cs/bs架构的区别

    Client/Server是建立在局域网的基础上的,基于客户端/服务器,安全,响应快,维护难度大,不易拓展,用户面固定,需要相同的操作系统. Browser/Server是建立在广域网的基础上的,基于 ...

  7. FlaskCBV视图类

    路由视图类 from flask import Flask app = Flask(name) 视图类 Views文件 看views源码 继承最后一个类 导入CBV的视图基类 from flask i ...

  8. oracle中start with和connect by的用法理解

    转自:https://blog.csdn.net/qq_29274091/article/details/72627350 Oracle中start with和connect by 用法理解转自:ht ...

  9. 缓冲区溢出漏洞 ms04011

    DSScan使用 扫描目标主机是否存在ms04011漏洞 getos使用 获取操作系统类型 > getos.exe 192.168.1.101 ------------------------- ...

  10. Linux防火墙Firewall-cmd 基础

    一.简介 ​ firewall-cmd 是firewalld服务的一个命令行客户端,提供了对防火墙规则的增删查改.firewalld自身并不具备防火墙的功能.它和iptables一样需要通过内核net ...