LeetCode 951. Flip Equivalent Binary Trees
方法一:递归
树的题,大部分都是递归。这道题的话,和same tree很像,无非就是加个判断看看是不是翻转的。
时间复杂度:T(n) = 4T(n/2), by Master Theorem, 时间复杂度 O(n^2)
空间复杂度:O(h)
class Solution { public:bool flipEquiv(TreeNode* root1, TreeNode* root2) {if (root1==NULL && root2==NULL) return true;if (root1==NULL || root2==NULL) return false;// here root1 not null, root2 not nullif (root1->val!=root2->val) return false;return flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right)|| flipEquiv(root1->left, root2->right) && flipEquiv(root1->right, root2->left);} };
方法二:Serialize Tree
Serialize Tree方法很多,可以参考那一道题。这里就用preorder。
考虑到翻转,所以序列化的时候,dfs的时候,左右儿子中val小的先遍历,这样就算有翻转的情况,最后serialize的结果也是一样的。
class Solution { public:bool flipEquiv(TreeNode* root1, TreeNode* root2) {return serialize(root1)==serialize(root2);}string serialize(TreeNode *root){if (root==NULL) return "#";string res=to_string(root->val);int left_val=root->left!=NULL?root->left->val:-1;int right_val=root->right!=NULL?root->right->val:-1;if (left_val<right_val)res += ' ' + serialize(root->left) + ' ' + serialize(root->right);elseres += ' ' + serialize(root->right) + ' ' + serialize(root->left);return res; } };
时间复杂度:和遍历树类似,O(n)
空间复杂度:serialize后字符串需要占空间。
转载于:https://www.cnblogs.com/hankunyan/p/10117408.html
LeetCode 951. Flip Equivalent Binary Trees相关推荐
- leetcode951. Flip Equivalent Binary Trees
题目链接 题目:给出两棵树,问能不能通过翻转(互换)左右孩子节点而互相转化 思路:递归进行比较,第一棵树的左孩子和第二棵数的左孩子,第一棵树的右孩子和第二棵数的右孩子 或者 第一棵树的左孩子和第二棵数 ...
- 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 ...
- [leetcode] 617. Merge Two Binary Trees
题目描述 给定两棵树,要求合并它们,如果节点重合则新合并的树的节点为它们之和,否则不变. 思路 简单递归,c语言怎么创建新节点忘记了,就直接写的. 代码 /*** Definition for a b ...
- leetcode 617. Merge Two Binary Trees | 617. 合并二叉树(Java)
题目 https://leetcode-cn.com/problems/merge-two-binary-trees/ 题解 测试用例 输入: [1,3,2,5] [2,1,3,null,4,null ...
- leetcode 971. Flip Binary Tree To Match Preorder Traversal
leetcode 971. Flip Binary Tree To Match Preorder Traversal 题意:给一颗二叉树,再给一个的数组,能否通过交换两个左右两个子节点,使得二叉树的前 ...
- LeetCode之All Possible Full Binary Trees(Kotlin)
问题: A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list o ...
- C#LeetCode刷题之#617-合并二叉树(Merge Two Binary Trees)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4096 访问. 给定两个二叉树,想象当你将它们中的一个覆盖到另一个 ...
- Rosalind第88题:Counting Rooted Binary Trees
Problem As in the case of unrooted trees, say that we have a fixed collection of taxa labeling the ...
- Tweaked Identical Binary Trees - Medium
Determine whether two given binary trees are identical assuming any number of 'tweak's are allowed. ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
最新文章
- Devexpress Xtraform 资源文件 汉化
- mysql检查备份数据脚本并在zabbix上告警
- java工具配置_Java™ 教程(配置实用工具)
- Vue — 第五天(路由)
- 好久没发胡说八道的贴了,今天发一贴
- vue-axios interceptors
- 输入一个浮点数,并输出该数的整数部分和小数部分
- 暴风集团:9月21日起公司股票交易进入退市整理期
- Docker日志自动化: ElasticSearch、Logstash、Kibana以及Logspout
- Atitit 函数式编程与命令行语言的区别与优缺点 目录 1. 常见的函数式语言	2 1.1. 命令行 bat	2 1.2. Sql h5 css 正则表达式	2 1.3. 工作流语言 anno注
- 360浏览器极速模式对iframe的支持
- 计算机中库的创建方法,win7系统下库的创建方法
- 吃瓜 || 一文看懂BCH分叉事件始末
- 高德AR驾车导航解决方案
- php cookie 注入,LiveZilla 'setCookieValue()'函数PHP对象注入漏洞
- 宇视摄像机/硬盘录像机等设备接入到国标GB28181协议视频平台EasyGBS的注意事项
- Cadence 17.4 中文菜单
- 用户体验的要素pdf_好的用户体验应该具备四要素
- realmeq参数配置详情_realmeq参数配置-realmeq手机性能规格详情
- 使用LSTM完成简单的中英翻译