0

I'm asked a question, when I traverse my binary tree, is it possible to get the node above the current node ? As a double linked list.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Marc Lamberti
  • 763
  • 2
  • 9
  • 24

1 Answers1

2

If you build it as a double-linked, then yes, go to the "parent" property. abstract example:

struct node {
    struct node *parent; // << this is the parent, just access it
    struct node *rchild;
    struct node *lchild;
    int val;
}

Else, you will need to cache the previous node at each access to a child node.

Note that a doubly link list is not the same as binary tree (in list each item has one child).

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
MByD
  • 135,866
  • 28
  • 264
  • 277