[Offer] to prove safety face questions: tree and binary tree

Offer prove safety in a total of 11 questions about the plane tree and binary tree

7 interview questions: rebuild binary tree

Description Title: and enter the result in a preorder traversal of a binary tree in preorder traversal of the binary tree a rebuild. Suppose Results preorder traversal order and input of duplicate numbers are free. Before entering e.g. preorder traversal sequence {1,2,4,7,3,5,6,8} and {4,7,2,1,5,3,8,6} order traversal sequence, and the reconstructed binary tree return.

Basics: In a preorder traversal sequence binary tree, the value of the first number is always the root of the tree. In order traversal sequence, the value in the middle of the sequence of the root node, the node value of the left subtree root value on the left, the right value of the node located on the right subtree of the root node value.

Ideas: first traversed by the preamble sequence to get the value of the root node, then preorder traversal sequence, find the location of the root node in a preorder traversal sequence, then the left side of the root node of value is left sub-tree, located in the root the right of the value of the node is the right child. Finally, by recursively, using the same approach to build the left subtree and right subtree.

Interview questions 8: the next node of the binary tree

Description Title: Given a binary tree and a node which is, find the next node in a preorder traversal order and returns. Note that the node in the tree contains not only the left and right child nodes, the parent node contains a pointer pointing to.

Ideas:

If a node has a right child, then it's the next node is the most left child of its right sub-tree. In other words, starting from the right child node has been along the pointer to the left child node, we can find it in the next node.
If a node does not have the right subtree, and the node is left child of its parent node, then it's the next node is its parent.
If a node does not have the right subtree, and the node is the right child of its parent node, then we can always traverse up the pointer to the parent node until it finds a node is left child of its parent. If such node exists, then the parent node of this node is the next node we're looking for.

26 is inscribed: a sub tree

Description Title: two binary inputs A, B, B is judged not substructure A's. (Ps: we agreed empty tree is not a tree any substructure)

Ideas:

Step 1: Find the value of and B of the root of the tree as a node in the tree A, R. This is in fact traversing the tree, you can use the cycle can also be used recursively (code simple).

Step Two: A decision tree is a root node in R value is not the same as B and the tree structure. The same idea of ​​recursive, if the value of the root node of the tree and the value R B is not the same node, or an R subtree root node B and certainly not have the same tree node; if they are the same value, determined recursively the value of their respective left and right node is not the same. Recursive termination condition is that we reach a leaf node tree A or B of the tree.

27 is inscribed: binary tree mirror

Description Title: operation of a given binary tree, the binary tree is converted into a source image.

Thinking: Each node preorder traversal of the tree, if the traversed node has children, exchanged its two child nodes. After the completion of switching the left and right child nodes of all non-leaf nodes of the mirror tree is obtained.

28 is inscribed: symmetrical binary tree

Description Title: Please implement a function, a binary tree is used to determine not symmetrical. Note that if a binary image is a binary tree with this same definition as symmetrical.

Order traversal before thinking :() traversal traversal algorithm defined for a symmetrical preamble, which is to traverse the parent node, then traverse its right child, and finally traversing its left child. If the binary tree traversal preamble sequences and preamble symmetrical traversal sequence, as it indicates that the binary tree is symmetrical. But pay special attention to the case of the node is empty.

32 is inscribed: printing the binary tree from top to bottom

Description Title: Printing out from the top of each node of the binary tree, with the layer node from left to right printing.

Ideas :( queue) by way of analysis, we can see that the laws are printed upside down binary tree: each time you print a node of the time, if the node has a child node around, put them added to the end of the queue. Next, to remove the head of the queue node into the queue first, in front of the printing operation is repeated, until all nodes in the queue have been printed.

Outward Bound 1: The binary tree print into multiple lines.

Description Title: binary print layer from top to bottom, from left to right with the output layer node. Each line of output layer.

Ideas :( queue) uses the same storage node queue to be printed. We define two variables represents a number of nodes of the current layer not printed, a number of nodes of the next layer representation. When the number of the current printed layer is 0, the next level of nodes assigned to the current number of printing layers, and is set to 0, and then printing the next layer.

Outward Bound 2: zigzag print order binary tree

Description Title: Implement a binary printing function according to a zigzag, i.e., the first row from left to right order of printing, the print order of the second layer is from right to left, left to right in the third row print order, other lines and so on.

Ideas :( stack) by way of analysis, we can discover the laws of a binary tree in a zigzag print order: requires two stacks. When we print a layer of nodes to save the child nodes corresponding to the next layer of the stack. If the currently printed layer is odd, the left child node and then to save the saved right child node to a first the stack; if the currently printed layer is even, the right child node to then save the saved left child node to the second stack in.

33 is inscribed: subsequent binary search tree traversal sequence

Description Title: Enter an integer array, the array is not the result of the determination after traversing a binary search tree. If the output Yes, otherwise the output No. Suppose the input array of any two numbers are different from each other.

Basics: the sequence preorder obtained, the last number is the value of the root node of the tree. Preceding numbers in an array can be divided into two parts: The first part is the value of the left sub-tree nodes, the root node than their small value; the second part is the value of the right subtree of a node, the root node than the value of their large.

Ideas:

If the sequence is empty or the array length is less than or equal to 0, return false;
we can get a sequence root according to the characteristics of postorder traversal, the left subtree and right subtree. In the traversal, the value of left child node of the tree to get recognition than small root, right subtree if found to have a value smaller than the root node, return false;
we define two Boolean values left and right, by Analyzing the same manner as are the left and right sub-tree is not binary search tree.
Finally, it returns a Boolean value (left && right)

34 is inscribed: a binary tree and a path to a value of

Description Title: input nodes and a binary tree with an integer, to print out all paths in the binary tree nodes as an input value and an integer. Forming a path to the path definition begins from the root node of the tree down to the leaf node has been traversed nodes. (Note: the return value in the list, a large array Array front)

Thinking :( stack) by way of example, we find that the law: when the front of a preorder traversal to access a node, we add the node to the path, and the accumulated value of the node. If the node is a leaf node, and the path and node value just equal to the integer input, the current path to meet the requirements, we print it out. If the current node is not a leaf node, we continue to access its child nodes. After the end of the current node access, recursive function will automatically return to its parent node. Therefore, we want to remove before the function exits on the path to the current node and subtracting the value of the current node, the parent node to ensure the return path happens to be from the root to the parent node. (With a stack to save the path)

37 is inscribed: Binary Serialization

Description Title: Implement the two functions, are used to serialize and deserialize binary tree.

Ideas: serialization and deserialization - l preamble traversal recursion, is converted to a null pointer, "$", includes left and right leaf node pointer is also converted to "$."

Large K-node binary search tree: 54 face questions

Topic Description: Given a binary search tree, find the k-th largest nodes therein. For example, (5,3,7,2,4,6,8), the numerical values ​​of the nodes according to the third largest value of node 4.

Thinking :( preorder) if traversing a binary search tree traversal sequence in the order of traversal is a sequence of values ​​in ascending order.

55 interview questions: binary tree of depth

Description Title: input a binary tree, find the depth of the tree. Forming a path tree from the root node to the leaf node sequentially passes (including the root, leaf nodes), the depth of the length of the longest path in the tree.

Recursive thinking :() If only one node of a tree, its depth is 1. If the root node has a left or right child sub-tree or tree has, the greater its depth is about the value of the sub-tree of depth plus 1. (Depth left and right subtrees obtained by recursively.)

Outward Bound: balanced binary tree

Description Title: input a binary tree, the binary tree is determined whether a balanced binary tree.

Basics: balanced binary tree must meet two conditions: a difference in height) left and right subtrees not be greater than 1; 2) each of the left and right subtrees below the root node must satisfy properties balanced binary tree.

Order traversal after thinking :() our binary tree traversal of each node by traversing the way, then traverse to a node when it has traversed the left and right sub-tree. As long as it records the depth of traversing each node, we can traverse the side, the side judge each node is not balanced.

END

Guess you like

Origin blog.csdn.net/Dora_5537/article/details/90721341