Authored by
undefined's avatar

An algorithm problem about React Context

  - accepted / - tried

If you use React, you must be familiar with Context. I've explained how React Context works internally.

You are given a binary tree which has nodes as blow.

type Node = {
  val: number,
  left: Node | null,
  right: Node | null
  nodeType: 'Normal' | 'Provider' | 'Consumer'

The type decides how the node should behavior when traversed. Now please implement a preorder traversal of such structure.

  1. Consumer's value should not be used, rather we should use the value from nearest ancestor Provider when traversed
  2. Provider's value should be ignored in the result.
  3. input is valid, all Consumers have ancestor Providers

For the above tree, your traverse() should return [1, 4, 2, 8, 5, 3, 6, 6]