문제

Given a preorder traversal of a full binary tree, where each node is labeled either a leaf node or an internal node, is there a good algorithm to find the height of the tree? For example, if N represents an internal node and L represents a leaf, then given the preorder traverseal NLNNLLL, the height would be three.

도움이 되었습니까?

해결책

Alright, I can't help but feel bad that we're leaving marti hanging in the comments. I think he truly doesn't know where to start, and has at least demonstrated that he's thought about the problem.

What do we know about a full binary tree? Each node is either a leaf or has two children.

A preorder traversal recursively visits the root, left subtree, then right subtree.

Think about this question: at what point in the preorder traversal (of a full binary tree) do we know we've exhausted a subtree? We will have visited its root, and then two leaves (or just the root if it's a leaf).

Let's make a stack of a special structure:

struct StackNode{
   size_t count; //initialize to 0
   char nodeType; //'N' or 'L'
};

This 'StackNode' object will track what type of node we visited in our preorder traversal using the 'nodeType' variable, which should be clear. We also have a special counter 'count' which we initialize to 0.

The idea behind a solution would be this:

  • each time you encounter a 'N', create a StackNode, and push it onto the stack.
  • each time you encounter a 'L', create a StackNode, and push it onto the stack
  • if the last node you pushed onto the stack was 'L', then pop the last node off, and then increment stack.top()'s count by 1
  • if stack.top()'s count is 2, then pop the top off the stack, and then increment stack.top()'s count by 1 (repeat until stack is empty or you've stopped popping off the stack)

Every time you push a node onto the stack, you can check the current height of your tree. It is the number of items in your stack-1 (accounting for the item on the bottom being the root).

As long as your track the maximum height you've encountered thus far, you will find the height of your tree.

Let's work through your example: NLNNLLL


Stack is initially empty.

int maxHeight = -1;

process first character: N

push a node onto the stack:

Stack: Type Count

  • N, 0

    maxHeight = 0;


process next character: L

push a node onto the stack:

  • L, 0
  • N, 0

    maxHeight = 1; //(incremented by 1)

The last character processed was a leaf, so pop and increment:

stack:

  • N, 1

    maxHeight = 1;


process next character: N

push a node onto the stack:

  • N, 0
  • N, 1

    maxHeight = 1; //unchanged


process next character: N

push a node onto the stack:

  • N, 0
  • N, 0
  • N, 1

    maxHeight = 2; //(incremented by 1)


process next character: L

push a node onto the stack:

  • L, 0
  • N, 0
  • N, 0
  • N, 1

    maxHeight = 3; //incremented by 1

last node was a leaf, so pop and increment

stack:

  • N, 1
  • N, 0
  • N, 1

    maxHeight = 3; //unchanged


process next character: L

push a node onto the stack:

  • L, 0
  • N, 1
  • N, 0
  • N, 1

    maxHeight = 3; //unchanged

last node was a leaf, so pop and increment:

  • N, 2
  • N, 0
  • N, 1

top node has count 2, so pop and increment:

  • N, 1
  • N, 1

process next node: L

push a node onto the stack:

  • L, 0
  • N, 1
  • N, 1

    maxHeight = 3; //unchanged

last node was a leaf, so pop and increment:

  • N, 2
  • N, 1

top node has count 2, so pop and increment:

  • N, 2

top node has count 2, so pop and increment:

(empty stack), finished

maxHeight = 3; //the maximum height discovered during a preorder of a full binary tree
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top