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.

  • 相关阅读:
    基础才是重中之重~stream和byte[]的概念与转化
    微信扫码i支付~官方DEMO的坑
    知方可补不足~SqlServer自动备份数据库及清理备份文件
    11g r2 模拟OCR和voting disk不可用,完整恢复过程,以及一些注意事项
    自定义navigationBar的高度
    Python 中的用户自定义类型
    hdu1370-Biorhythms
    Ruby on Rails 實戰聖經阅读(三)
    重新配置与卸载 11gR2 Grid Infrastructure
    非确定有限状态自动机的构建(一)——NFA的定义和实现
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11117809.html
Copyright © 2011-2022 走看看