Jul. 24th, 2016

Тут хтось поскаржився, що порядок обчислень в Х-і не зрозумілий. Ну, ось вам імперативний варіант, не обляпайтесь:
function walk2(preorder, inorder) {
  let node = {parent: {}};
  while(preorder.length !== 0) {
    if (preorder[preorder.length - 1] === node.parent.value) {
      preorder.pop();
      node.parent.left = node;
      node = node.parent;
      continue;
    }
    node.parent = {value: inorder.pop(), parent: node.parent, right: node};
    node = {parent: node.parent};
  }
  return node;
}
Мені аж цікаво, як я раніше в такому розбирався?

Profile

sassa_nf

February 2026

S M T W T F S
1234567
891011121314
15161718192021
222324252627 28

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 22nd, 2026 05:51 pm
Powered by Dreamwidth Studios