zoukankan      html  css  js  c++  java
  • 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. 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:

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     List<Integer> res = new ArrayList<Integer>();
    12     int i = 0;
    13     
    14     public List<Integer> flipMatchVoyage(TreeNode root, int[] voyage) {
    15         return dfs(root, voyage) ? res : Arrays.asList(-1);
    16     }
    17     
    18     private boolean dfs(TreeNode root, int[] voyage){
    19         if(root == null){
    20             return true;
    21         }
    22         
    23         if(root.val != voyage[i]){
    24             return false;
    25         }
    26         
    27         i++;
    28         if(root.left != null && root.left.val != voyage[i]){
    29             res.add(root.val);
    30             return dfs(root.right, voyage) && dfs(root.left, voyage);
    31         }
    32         
    33         return dfs(root.left, voyage) && dfs(root.right, voyage);
    34     }
    35 }

    Need practice Again.

  • 相关阅读:
    501. 二叉搜索树中的众数
    530. 二叉搜索树的最小绝对差
    Java强制类型转换注意
    PL_SQL常用快捷键及设置
    Java线程:大总结
    Java多线程售票程序分析
    Java多线程的几种实现方式
    Java线程的几种状态
    Java的同步机制:synchronized关键字
    Eclipse常用设置[不断完善]
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11117809.html
Copyright © 2011-2022 走看看