【leetcode】951. Flip Equivalent Binary Trees

题目如下:

For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.

A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations.

Write a function that determines whether two binary trees are flip equivalent.  The trees are given by root nodes root1 and root2.

Example 1:

Input: root1 = [1,2,3,4,5,6,null,null,null,7,8], root2 = [1,3,2,null,6,4,5,null,null,null,null,8,7]
Output: true
Explanation: We flipped at nodes with values 1, 3, and 5.

Note:

  1. Each tree will have at most 100 nodes.
  2. Each value in each tree will be a unique integer in the range [0, 99].

解题思路:从两个根节点开始分别同步遍历两棵树,如果左右子树相同则表示不用交换;如果左右互相等于对方则交换;否则表示无法交换。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    res = True
    def verify(self,node1,node2):
        leftV1 = None if node1.left == None else node1.left.val
        leftV2 = None if node2.left == None else node2.left.val
        rightV1 = None if node1.right == None else node1.right.val
        rightV2 = None if node2.right == None else node2.right.val
        if leftV1 == leftV2 and rightV1 == rightV2:
            return 0
        elif leftV1 == rightV2 and rightV1 == leftV2:
            return 1
        else:
            return -1
    def traverse(self,node1,node2):
        if node1 == None or node2 == None:
            return
        ret = self.verify(node1,node2)
        if ret == 0:
            self.traverse(node1.left,node2.left)
            self.traverse(node1.right, node2.right)
        elif ret == 1:
            node2.left,node2.right = node2.right,node2.left
            self.traverse(node1.left, node2.left)
            self.traverse(node1.right, node2.right)
        else:
            self.res = False

    def flipEquiv(self, root1, root2):
        """
        :type root1: TreeNode
        :type root2: TreeNode
        :rtype: bool
        """
        if (root1 == None) ^ (root2 == None):
            return False
        self.res = True
        self.traverse(root1,root2)
        return self.res

原文地址:https://www.cnblogs.com/seyjs/p/10084253.html

时间: 2024-07-29 02:33:06

【leetcode】951. Flip Equivalent Binary Trees的相关文章

LeetCode 951. Flip Equivalent Binary Trees

原题链接在这里:https://leetcode.com/problems/flip-equivalent-binary-trees/ 题目: For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. A binary tree X is flip equivalent to a binary tree Y

113th LeetCode Weekly Contest Flip Equivalent Binary Trees

For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip opera

【LeetCode】 Maximum Depth of Binary Tree

Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【Leetcode_easy】617. Merge Two Binary Trees

problem 617. Merge Two Binary Trees 参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11076297.html

【LeetCode】105 & 106. Construct Binary Tree from Inorder and Postorder Traversal

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 提示: 题目要求通过一颗二叉树的中序遍历及后续遍历的结果,将这颗二叉树构建出来,另外还有一个已知条件,所有节点的值都是不同的. 首先需要了解一下二叉树不同遍历方式的定义: 前序遍历:首先访问根结点,然后遍历左子树,最

【leetcode】Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 利用递归. 也可以使用栈和队列,即使用DFS和BFS方法. C++: 1 /** 2 * Definition for binary tree 3 * struct TreeNod

【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and Postorder' (Problem 106), u need build the binary tree. Input: 105. Preorder & Inorder traversal 106. Inorder & Postorder traversal output: A binar

【leetcode】1072. Flip Columns For Maximum Number of Equal Rows

题目如下: Given a matrix consisting of 0s and 1s, we may choose any number of columns in the matrix and flip every cell in that column.  Flipping a cell changes the value of that cell from 0 to 1 or from 1 to 0. Return the maximum number of rows that hav