971. Flip Binary Tree To Match Preorder Traversal [Medium]

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
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

算法思路:

采用先序遍历的时候当前节点值与voyage中的值不一样,直接返回false,如果该节点的左节点存在且值与voyage中的不一样,需要flip一下继续先序遍历,否则直接继续先序遍历(值相同)。

这里的preorder函数也可以不用返回布尔值,直接在在值不想等的时候,清空res后放入-1,然后返回,在主函数中判断下,如果返回空或者不是不空时首元素不是-1,直接返回,否则清空,返回-1。为什么呢?因为递归调用中,清空放入-1后,这个分支结束,但是其他分支可能继续放如flip节点值进入res了,所以需要判断下,或者在每次进入下一层先序遍历的时候先判断res不为空且res[0] != -1也可。

class Solution {
public:
    vector<int> flipMatchVoyage(TreeNode* root, vector<int>& voyage) {
        int i = 0;
        vector<int> res;
        bool canFilpMatch = preorder(root, voyage, i, res);
        return !canFilpMatch ? vector<int>(1, -1) : res;
    }   
private:
    bool preorder(TreeNode* node, vector<int>& voyage, int& i, vector<int>& res) {
        if (node == nullptr) return true;
        if (node->val != voyage[i++]) return false;
        // 注意:必须先进行顶层的flip,因为顶层的flip不仅仅是左右子节点两个值,而是涉及到下面的整个子树
        bool l, r;
        if (node->left && node->left->val != voyage[i]) { // flip
            res.push_back(node->val);
            l = preorder(node->right, voyage, i, res);
            r = preorder(node->left, voyage, i, res);
        } else {
            l = preorder(node->left, voyage, i, res);
            r = preorder(node->right, voyage, i, res);
        }
        return (l && r);
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值