Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Tree problems in depth

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Tree problems in depth

Populating Next Right Pointers in Each Node

advancedlayer I2 · Tree problems in depth5 tests

Every node in this perfect binary tree (all leaves at the same depth, every other node has two children) carries an extra next pointer, initially null. Set each next to the node immediately to its right on the same level, and return the root.

  • The last node of every level keeps next = null
  • Annotate the given tree in place and return the same root
  • The next pointers you have already set can be used to walk a level — that is how you avoid a queue
  • nextChain(node) follows next from a node and collects the values

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen