Home java java. Help to understand the task with the binary tree

java. Help to understand the task with the binary tree

Author

Date

Category

Do not quite understand how to build a tree itself.

In this task, binary trees are considered. Figure below shows an example of a binary tree consisting of seven nodes.

Binary tree is either an empty tree or a node (called root) containing one integer value and associated with two other binary trees. We are interested in the paths (sequences of related adjacent nodes) that begin in the root and follow the edges of the tree (marked with arrows in the figure above). For example, the sequence of nodes A, B, D is a permissible path, and the sequence A, B, G-no.

We want to find the maximum number of different values ​​that appear on the way, ranging from the root of the tree. For example, on the path consisting of nodes A, B, D, G, there are two different values ​​(4 and 5). On the way A, C, E there are three different values ​​(1, 4 and 6).
There is no way containing four or more different values.

Write a feature:
Solution of class {Public Int Solution (Tree T); }

This, given the binary T Tree T, consisting of n nodes, returns the maximum number of different values ​​that appear on the path starting with the root of Tree T. For example, given the tree shown above, the function must return 3.

technical details

Binary tree is set using the data structure of the pointer. Suppose that the following ads are given:

Public Class Tree {
  Public int x;
  Public Tree L;
  Public Tree R;
}

The empty tree is represented by an empty pointer (denotes NULL). A non-empty tree is represented by a pointer to an object representing its root. The X attribute contains an integer contained in the root, while the attributes L and R contain the left and right support of the binary tree, respectively.

Prejudice

Write an effective algorithm for the following assumptions:
* N-integer in the range [1..50,000];
• Tree height (the number of ribs on the longest path from the root to the sheet) is in the range [0..3,500];
• Each value in the T tree is an integer in the range [1.]


@ name Familia thought this way

Public Void Preorder (Tree Tree) {
    if (Tree == NULL) {
      Return;
    }
// here to do something with a leaf value
    Preorder (Tree.l);
    Preorder (Tree.r);
  }
}

Answer 1, Authority 100%

For convenience, you can make two constructors: to create a node with a number and node with connected nodes.

Public Class Tree {
  Public int x;
  Public Tree L;
  Public Tree R;
  Public Tree (int x) {
    this.x = x;
  }
  Public Tree (int x, Tree L, Tree R) {
    this.x = x;
    this.l = L;
    this.r = R;
  }
}

Creating a tree will look like this:

Tree Tree = New Tree (4,
        NEW TREE (2,
            NEW TREE (5), NULL),
        NEW TREE (2,
            NEW TREE (3,
                NEW TREE (5),
                NEW TREE (4)),
            NEW TREE (2)));

In this way, you can get around the whole tree and get the maximum number of different numbers of nodes in the tree:

hashset & lt; integer & gt; gatheredvalues ​​= new hashset & lt; & gt; ();
Public Int Solution (Tree Tree) {
  if (Tree == NULL) RETURN GATHEREDVALUES.SIZE ();
  Boolean NotContains =! gatheredvalues.contains (Tree.x); 
if (notcontains) gatheredvalues.add (tree.x);
   if (gatheredvalues.size () == 3) Return 3;
   // INT MAXVALUE = Math.max (Solution (Tree.l), Solution (Tree.R)). Replaced to save time (nodes still can be up to 50 thousand!)
   INT LVALUE = Solution (Tree.l);
   INT RVALUE = Solution (Tree.R);
   int maxvalue = (lvalue & gt; = rvalue)? LVALUE: RValue;
   if (notcontains) gatheredvalues.remove (time.x);
   RETURN MAXVALUE;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions