diff options
author | Jack Sangdahl <[email protected]> | 2024-10-23 21:31:55 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-10-23 21:31:55 -0600 |
commit | 0297643f9bc94881eb3ea3b3f91741fcf34a4d7d (patch) | |
tree | 06abdc297108330fd2b5cc1c9044c1eed352e068 | |
parent | 9b1afed7980a5eb2b1f4bd886d07340629fde40c (diff) | |
download | leetcode-0297643f9bc94881eb3ea3b3f91741fcf34a4d7d.tar.gz leetcode-0297643f9bc94881eb3ea3b3f91741fcf34a4d7d.zip |
951. flip equivalent binary trees
-rw-r--r-- | flip_equivalent_binary_tree.c | 94 | ||||
-rw-r--r-- | flip_equivalent_binary_tree.py | 57 |
2 files changed, 151 insertions, 0 deletions
diff --git a/flip_equivalent_binary_tree.c b/flip_equivalent_binary_tree.c new file mode 100644 index 0000000..fedfe5c --- /dev/null +++ b/flip_equivalent_binary_tree.c @@ -0,0 +1,94 @@ +// 951. Flip Equivalent Binary Trees +#include "leetcode.h" + +/* + * for a binarytree 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. given the roots of two binary trees 'root1' + * and 'root2', return 'true' if the two trees are flip equivalent. + */ + +//__attribute((naked)) +bool flipEquiv(struct TreeNode *root1, struct TreeNode *root2) { + /* + __asm("push %rbp;" + "mov %rsp,%rbp;" + "push %rbx;" + "push %r8;" + "push %r9;" + "xor %eax,%eax;" + "test %rdi,%rdi;" + "jnz l1;" + "test %rsi,%rsi;" + "jnz l0;" + "add $0x1,%eax;" + "jmp l0;" + "l1:;" + "test %rsi,%rsi;" + "jz l0;" + "mov (%rdi),%ecx;" + "cmp %ecx,(%rsi);" + "jne l0;" + "mov %rdi,%r8;" + "mov %rsi,%r9;" + "mov 0x8(%r8),%rdi;" + "mov 0x8(%r9),%rsi;" + "call flipEquiv;" + "xchg %eax,%ebx;" + "mov 0x10(%r8),%rdi;" + "mov 0x10(%r9),%rsi;" + "call flipEquiv;" + "and %ebx,%eax;" + "jnz l0;" + "mov 0x8(%r8),%rdi;" + "mov 0x10(%r9),%rsi;" + "call flipEquiv;" + "xchg %eax,%ebx;" + "mov 0x10(%r8),%rdi;" + "mov 0x8(%r9),%rsi;" + "call flipEquiv;" + "and %ebx,%eax;" + "l0:;" + "pop %r9;" + "pop %r8;" + "pop %rbx;" + "mov %rbp,%rsp;" + "pop %rbp;" + "pop %rcx;" + "jmp %rcx;"); + */ + if (!root1 || !root2) + return root1 == root2; + if (root1->val == root2->val) { + return (flipEquiv(root1->left, root2->right) & + flipEquiv(root1->right, root2->left)) || + (flipEquiv(root1->left, root2->left) & + flipEquiv(root1->right, root2->right)); + } + return false; +} + +int main() { + struct TreeNode *r1 = treenode_create(1); + r1->left = treenode_create(2); + r1->right = treenode_create(3); + r1->left->left = treenode_create(4); + r1->left->right = treenode_create(5); + r1->right->left = treenode_create(6); + r1->right->right = NULL; + r1->left->right->left = treenode_create(7); + r1->left->right->right = treenode_create(8); + struct TreeNode *r2 = treenode_create(1); + r2->left = treenode_create(3); + r2->right = treenode_create(2); + r2->left->left = NULL; + r2->left->right = treenode_create(6); + r2->right->left = treenode_create(4); + r2->right->right = treenode_create(5); + r2->right->right->left = treenode_create(8); + r2->right->right->right = treenode_create(7); + printf("%d\n", flipEquiv(r1, r2)); // expect: 1 + treenode_free(r1); + treenode_free(r2); +} diff --git a/flip_equivalent_binary_tree.py b/flip_equivalent_binary_tree.py new file mode 100644 index 0000000..0d80f62 --- /dev/null +++ b/flip_equivalent_binary_tree.py @@ -0,0 +1,57 @@ +# 951. Flip Equivalent Binary Trees + +""" +for a binarytree 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. given the roots of two binary trees 'root1' +and 'root2', return 'true' if the two trees are flip equivalent. +""" + + +# Definition for a binary tree node. +class TreeNode(object): + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution(object): + def flipEquiv(self, root1, root2): + """ + :type root1: Optional[TreeNode] + :type root2: Optional[TreeNode] + :rtype: bool + """ + s1, s2 = [root1], [root2] + while s1 and s2: + n1, n2 = s1.pop(), s2.pop() + if n1 is None or n2 is None: + continue + elif not n1 or not n1 or n1.val != n2.val: + return False + if ( + n1.left is None + or n2.left is None + or n1.left + and n2.left + and n1.left.val == n2.left.val + ): + s1.extend([n1.left, n1.right]) + else: + s1.extend([n1.right, n1.left]) + s2.extend([n2.left, n2.right]) + return not s1 and not s2 + + +if __name__ == "__main__": + obj = Solution() + print( + obj.flipEquiv( + root1=[1, 2, 3, 4, 5, 6, None, None, None, 7, 8], + root2=[1, 3, 2, None, 6, 4, 5, None, None, None, None, 8, 7], + ) + ) + print(obj.flipEquiv(root1=[], root2=[])) + print(obj.flipEquiv(root1=[], root2=[1])) |