Leetcode 951:翻转等价二叉树
2019-03-08 22:51
399 查看
题目描述
我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树。
只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉树 Y。
编写一个判断两个二叉树是否是翻转等价的函数。这些树由根节点
root1和
root2给出。
示例:
输入: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] 输出:true 解释:We flipped at nodes with values 1, 3, and 5.
提示:
- 每棵树最多有
100
个节点。 - 每棵树中的每个值都是唯一的、在
[0, 99]
范围内的整数。
解题思路
我们使用root2 作为参照。 只需要对于每一个节点判断 正常情况下是否是一样的 以及翻转之后是否是一样的。
[code]bool flipEquiv(TreeNode* root1, TreeNode* root2) { if(root1 == NULL && root2 == NULL) return true; if((root1 != NULL && root2 == NULL)||(root1 == NULL && root2 != NULL)) return false; if(root1->val != root2->val) return false; return (flipEquiv(root1->left,root2->left)&&flipEquiv(root1->right,root2->right))||(flipEquiv(root1->right,root2->left)&&flipEquiv(root1->left,root2->right)); }
相关文章推荐
- leetCode之翻转二叉树
- 【LeetCode】Identical Binary Tree等价二叉树 - Easy+
- leetcode 226 Invert Binary Tree 翻转二叉树
- leetcode 226 Invert Binary Tree 翻转二叉树
- leetcode - 226 - 翻转二叉树
- [LeetCode] 226 翻转二叉树
- leetcode 226 Invert Binary Tree 翻转二叉树
- 【LeetCode】Invert Binary Tree 翻转二叉树 - Easy++
- LeetCode226--翻转二叉树
- 【LeetCode】226. Invert Binary Tree 二叉树翻转
- [LeetCode] Invert Binary Tree 翻转二叉树
- 翻转二叉树(leetcode-226)
- [LeetCode] Invert Binary Tree - 二叉树翻转系列问题
- LeetCode 226 Invert Binary Tree(翻转二叉树)
- [数据结构][Leetcode]翻转二叉树
- LeetCode Invert Binary Tree 翻转二叉树
- [LeetCode] Equal Tree Partition 划分等价树
- 等价二叉树
- Leetcode 124 Binary Tree Maximum Path Sum 二叉树最大路径和
- LeetCode222 Count CompleteTree Nodes(计算全然二叉树的节点数) Java 题解