1. 程式人生 > >[Swift Weekly Contest 118]LeetCode971.翻轉二叉樹以匹配先序遍歷 | Flip Binary Tree To Match Preorder Traversal

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

給定一個有 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 }