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.

  • 相关阅读:
    我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比(转载)
    spring boot 使用application.properties 进行外部配置
    Nginx+Springboot+Vue 前后端分离 解决跨域问题
    windows7下RabbitMQ的安装
    七牛云 如何配置域名的 CNAME
    springboot接口 接收参数为实体对象跟MultipartFile对象报错。
    数据库性能优化之SQL语句优化(下)
    数据库性能优化之SQL语句优化(上)
    七个对我最好的职业建议
    程序员最艰巨的十大任务
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11117809.html
Copyright © 2011-2022 走看看