-
Answer: c. The storage structure with a binary linked list is also the storage structure of the left child and the right brother.
Post-order traversal is reasonable. The normal logic should be: do a good job of swapping within the current node subtree, and then swap the left and right subtrees of the current node. It just conforms to the algorithmic logic of post-order traversal.
1. Swap the left subtree.
2. Swap the right subtree.
3. Swap the left subtree and the right subtree.
Other algorithms, such as precedent and hierarchical, have a similar logic, i.e., swap the left and right subtrees of the current node when it is accessed. Logically, it's a little awkward. Therefore, the most appropriate should be post-order traversal, but from the implementation point of view, both pre-order and hierarchical are possible.
1. Swap the left subtree and the right subtree.
2. Traverse the left subtree.
3. Traverse the right subtree.
Traverse by level.
1. The root node is in the queue.
2. Out of the queue, swap the left and right subtrees, put the root of the subtree into queue 3, and repeat 2 until the queue is empty.
Medium-order traversal is a bit more difficult to implement.
-
Realistically speaking, the precedence is not okay!
-
Algorithm Steps:
Set the root node to r.
In case 1, if r has both left and right children, then return 1 + recursively find the left subtree as the number of nodes as 2 nodes + recursively find the right subtree as the number of nodes as 2 nodes.
In case 2, if r has only the left child, then return recursively to find the left subtree degree to be 2 nodes.
In case 3, if r has only the right child, then return recursively to find the right subtree degree to be 2 nodes.
Case 4, if r has neither a left child nor a right child, 0 is returned.
-
int issearchtree(const btnode *t)else if((t->rchild) &t->lchild))
else has been successfully verified on the computer.,The upstairs writing is too casual.,All kinds of situations need to be considered.。
-
Recursive methods.
void alvtree(bittree *t)else return 0;
else if (t->lchild!=null&&t->rchild==null)
else if (t->rchild!=null&&t->lchild==null)
The situation of writing by yourself should be taken into account, and the recursive algorithm is used.
-
This question examines the traversal of a binary tree.
There are a total of 4 traversals of the binary tree.
Pre-order traversal. Middle-order traversal.
Post-order traversal. Sequence traversal. Outline.
-
Summary. Kiss <>
Hello, let me assume that the binary tree t uses the binary chain as the storage structure, design an algorithm, and find the number of leaf nodes in the binary tree t assuming that the binary tree t uses the binary chain as the storage structure, design an algorithm to find the number of leaf nodes in the binary tree t The leaf nodes of the binary tree refer to the nodes without child nodes. Therefore, we can use a recursive approach to solve this problem. The algorithm process is as follows:
If the current node is empty, 0 is returned. If the current node is a leaf node, 1 is returned.
Assuming that the binary tree t uses the binary chain as the storage structure, an algorithm is designed to find the number of leaf nodes in the binary tree t.
Kiss <>
Hello, let me assume that the binary tree t uses the binary chain as the storage structure, design an algorithm, and find the number of leaf nodes in the binary tree t assuming that the binary tree t uses the binary chain as the storage structure, design an algorithm to find the number of leaf nodes in the binary tree t The leaf nodes of the binary tree refer to the nodes without child nodes. Therefore, we can use a recursive approach to solve this problem. The algorithm process is as follows:
If the current node is empty, 0 is returned. If the current node is a leaf node, 1 is returned.
Kiss <>
If the current node is not a leaf node, the number of leaf nodes in its left and right subtrees is recursively counted and they are added. Here's an algorithm implemented in C++: In this section, Struct TreeNode defines the structure of a binary tree node, with root->left and root->right representing the left and right subnodes of the current node, respectively.
Time complexity: o(n), where n is the number of nodes in the binary tree. Space Complexity:
o(h), where h is the height of the binary tree. Since recursion is used, the spatial complexity of the algorithm depends on the depth of the recursion stack, i.e., the height of the binary tree.
-
From the pre-order traversal to know that a is the root, then from the middle order we can know that the rest of the nodes are the left subtree of a, remove a, b is the root in the pre-order, then c is the left subtree, the rest is the right subtree, remove b c, then the pre-order d d is the root, then e f is the left subtree, the rest is the right subtree, ef in the order of the precedent is ef, then e is the root, from the middle order we know that f is the right subtree, and so on, we can know that the binary tree is:
-
This one. Practice and guess Rely on angle and strength to hit, this thing is not good, or play it yourself.
1. NaOH + CH3COOH = CH3COONA + H2O So when NaOH and CH3COOH react completely, the amount of NaOH and CH3COOH substances consumed is equal. >>>More
The original text of King Liang Hui said: "The widow is also to the country, and he is dedicated to his ears." Hanoi.
If two circles intersect at the points a(1,3) and b(m,-1), and the centers of the two circles are on the line x-y+c=0, what is the value of m+c? To write out the detailed process!! >>>More
It's a good deal to get to the back.
It would be nice to copy it. Ha ha. >>>More