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.

  • 相关阅读:
    var在PHP和JS中的使用
    修改PHP上传文件大小限制的方法
    Linux中tail指令详解
    drupal7 profile2模块获取个人信息
    drupal7 STMP邮件模块配置
    drupal读取mysql的longblob字段
    drupal7 自定义登录&找回密码页面,注意事项
    转 VS Code 快捷键大全,没有更全
    权力关进笼子里
    drupal的权限设置
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11117809.html
Copyright © 2011-2022 走看看