aboutsummaryrefslogtreecommitdiff
path: root/flip_equivalent_binary_tree.c
blob: fedfe5cb01fe317c29ed3ab1b8f4472631da4b74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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);
}