[Swift Weekly Contest 118]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 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 voyagewe 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


给定一个有 N 个节点的二叉树,每个节点都有一个不同于其他节点且处于 {1, ..., N} 中的值。

通过交换节点的左子节点和右子节点,可以翻转该二叉树中的节点。

考虑从根节点开始的先序遍历报告的 N 值序列。将这一 N 值序列称为树的行程。

(回想一下,节点的先序遍历意味着我们报告当前节点的值,然后先序遍历左子节点,再先序遍历右子节点。)

我们的目标是翻转最少的树中节点,以便树的行程与给定的行程 voyage 相匹配。

如果可以,则返回翻转的所有节点的值的列表。你可以按任何顺序返回答案。

如果不能,则返回列表 [-1]

示例 1:

输入:root = [1,2], voyage = [2,1]
输出:[-1]

示例 2:

输入:root = [1,2,3], voyage = [1,3,2]
输出:[1]

示例 3:

输入:root = [1,2,3], voyage = [1,2,3]
输出:[] 

提示:

  1. 1 <= N <= 100

20ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     var p:Int = 0
16     var fed:[Int] = [Int]()
17     func flipMatchVoyage(_ root: TreeNode?, _ voyage: [Int]) -> [Int] {
18         p = 0
19         if dfs(root, voyage)
20         {
21             return fed
22         }
23         else
24         {
25             fed = [Int]()
26             fed.append(-1);
27             return fed;
28         }
29     }
30
31     func dfs(_ cur: TreeNode?, _ voyage: [Int]) -> Bool
32     {
33         if cur == nil {return true}
34         if voyage[p] != cur!.val {return false}
35         p += 1
36         if cur!.left == nil
37         {
38             return dfs(cur?.right, voyage)
39         }
40         if cur!.right == nil
41         {
42             return dfs(cur?.left, voyage)
43         }
44
45         if voyage[p] == cur!.left!.val
46         {
47             var res:Bool = dfs(cur?.left, voyage)
48             if !res
49             {
50                 return false
51             }
52             return dfs(cur?.right, voyage)
53         }
54         else
55         {
56             fed.append(cur!.val)
57             var res:Bool = dfs(cur?.right, voyage)
58             if !res
59             {
60                 return false
61             }
62             return dfs(cur?.left, voyage)
63         }
64     }
65 }

原文地址:https://www.cnblogs.com/strengthen/p/10228410.html

时间: 2024-07-29 15:39:41

[Swift Weekly Contest 118]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal的相关文章

[Swift Weekly Contest 120]LeetCode979. 在二叉树中分配硬币 | Distribute Coins in Binary Tree

Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total. In one move, we may choose two adjacent nodes and move one coin from one node to another.  (The move may be from parent to child, or

[Swift Weekly Contest 117]LeetCode965. 单值二叉树 | Univalued Binary Tree

A binary tree is univalued if every node in the tree has the same value. Return true if and only if the given tree is univalued. Example 1: Input: [1,1,1,1,1,null,1] Output: true Example 2: Input: [2,2,2,5,2] Output: false Note: The number of nodes i

LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后序遍历的顺序是左孩子->右孩子->父节点,对于每个遍历到的节点,执行如下操作: 首先对该节点不断沿左孩子方向向下遍历并入栈,直到左孩子为空 取出栈顶节点,此时该节点为树的最左下节点,若其右孩子不为空,则回到步骤1:若为空说明其为叶子节点,将其值输出到结果中,并记录pre为当前节点 在步骤2判断右孩子是

[Swift Weekly Contest 118]LeetCode969.煎饼排序 | Pancake Sorting

Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, then reverse the order of the first k elements of A.  We want to perform zero or more pancake flips (doing them one after another in succession) to sort t

[Swift Weekly Contest 118]LeetCode970. 强整数 | Powerful Integers

Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0. Return a list of all powerful integers that have value less than or equal to bound. You may return the answer in any ord

[Swift Weekly Contest 118]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K

Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K. Example 1: Input: A = [4,5,0,-2,-3,1], K = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by K = 5: [4, 5, 0, -2

[Swift Weekly Contest 118]LeetCode976. 三角形的最大周长 | Largest Perimeter Triangle

Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths. If it is impossible to form any triangle of non-zero area, return 0. Example 1: Input: [2,1,2] Output: 5 Example 2: I

[Swift Weekly Contest 118]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). LeetCode103. Binary Tree Zigzag Level Order Traversal中等 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 返回锯齿形层次遍历如下: [