An algorithm problem about React Context
Authored by
JSer
- 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
For the above tree, your traverse()
should return [1, 4, 2, 8, 5, 3, 6, 6]
Loading...