Authored by
JSer
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.
- Consumer's value should not be used, rather we should use the value from nearest ancestor Provider when traversed
- Provider's value should be ignored in the result.
- input is valid, all Consumers have ancestor Providers
https://bigfrontend.dev/tools/excalidraw/embed/6245ce64-eddd-4a96-99ee-ae12e1966883?w=680&h=555
For the above tree, your traverse()
should return [1, 4, 2, 8, 5, 3, 6, 6]
Loading...