楼主:
JIWP (JIWP)
2024-10-24 22:47:55951. Flip Equivalent Binary Trees
给你两棵二元树
请问这其中一棵二元树的一些节点经过flip operation后
会不会跟另外一棵二元树相等
flip operation就是把一个node的左右节点交换
思路:
就把两棵树丢下去检查
如果两棵树的node都为null为传true
其中一个node为null或两个node的值不相等回传false
不是上面的情况
就接着检查有flip operation 或是 没有flip operation只要有一种情况是true
就回传true
golang code :
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func flipEquiv(root1 *TreeNode, root2 *TreeNode) bool {
if root1 == nil && root2 == nil {
return true
}
if root1 == nil || root2 == nil || root1.Val != root2.Val {
return false
}
return (flipEquiv(root1.Left, root2.Right) && flipEquiv(root1.Right, root2.
Left)) || (flipEquiv(root1.Right, root2.Right) && flipEquiv(root1.Left, root2.
Left))
}