FastPrepFlip Equivalent Binary Trees

Flip Equivalent Binary Trees

SambaNova Systems logoSambaNova Systems● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Two binary trees are flip equivalent if one can be transformed into the other by repeatedly swapping the left and right children of any node.

Return whether the two supplied trees are flip equivalent.

Function

flipEquiv(root1: TreeNode, root2: TreeNode) → boolean

Examples

Example 1

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]return = true

Flips at selected nodes align both trees.

Example 2

root1 = []root2 = []return = true

Two empty trees are equivalent.

Example 3

root1 = []root2 = [1]return = false

Only one tree is empty.

Constraints

  • Each tree has 0 to 100 nodes.
  • 0 <= node.val <= 100.

More SambaNova Systems problems

See SambaNova Systems hiring insights
public boolean flipEquiv(TreeNode root1, TreeNode root2) {
    // write your code here
}
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]
expectedtrue
Checking account…