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 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].

题解:

Compare root1 and root2, see if it is equal first.

If yes, then

Case 1: root1.left equivalent to root2.left && root1.right equivalent to root2.right

Case 2: root1.left equivalent to root2.right && root1.right equivalent to root2.left.

Either case is true, then return true.

Time Complexity: O(n^2). T(n) = 4*T(n/2) + 1. Master Theorem, T(n) = O(n^2).

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     public boolean flipEquiv(TreeNode root1, TreeNode root2) {
12         if(root1 == null && root2 == null){
13             return true;
14         }
15
16         if(root1 == null || root2 == null){
17             return false;
18         }
19
20         if(root1.val != root2.val){
21             return false;
22         }
23
24         return (flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right))
25             || (flipEquiv(root1.left, root2.right) && flipEquiv(root1.right, root2.left));
26     }
27 }

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11072771.html

时间: 2024-10-07 07:34:56

LeetCode 951. Flip Equivalent Binary Trees的相关文章

【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

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 #617 Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then

LeetCode 617. Merge Two Binary Trees合并二叉树 (C++)

题目: Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap,

【一天一道LeetCode】#96. Unique Binary Search Trees

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example, Given n = 3, there are a total of 5 unique BST's. (二)解题

leetcode第一刷_Unique Binary Search Trees

这道题其实跟二叉搜索树没有什么关系,给定n个节点,让你求有多少棵二叉树也是完全一样的做法.思想是什么呢,给定一个节点数x,求f(x),f(x)跟什么有关系呢,当然是跟他的左右子树都有关系,所以可以利用其左右子树的结论,大问题被成功转化成了小问题.最熟悉的方法是递归和dp,这里显然有大量的重复计算,用dp打表好一些. 后来实验的同学说,这其实是一个Catalan数,上网查了一下,果然啊.Catalan数是这样子的: h(0) = 1, h(1) = 1; 递推式:h(n)= h(0)*h(n-1)

leetcode第一刷_Unique Binary Search Trees II

http://acm.hdu.edu.cn/showproblem.php?pid=1507 大致题意:在一个n*m的格子上,黑色的地方不可用,问在白色格子上最多可放多少1*2的矩阵. 思路:建图,每个白色格子与它临近的上下左右的白色格子建边,求最大匹配,答案为最大匹配/2,因为是双向图.最后输出匹配边时,当找到一组匹配边记得将该边标记,以防重复计算. #include <stdio.h> #include <algorithm> #include <set> #inc

[LeetCode] Merge Two Binary Trees 合并二叉树

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then

[LeetCode] Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then