describe
Let the elements of each node in the binary tree be one character, establish a binary linked list in the order of first-order traversal, and write a recursive algorithm to calculate the height of the binary tree.
input
Multiple sets of data. One row of each group of data is the pre order sequence of binary tree (when the element in the sequence is' 0 ', it means that the node is empty). When the input has only one "0", the input ends.
output
Each group of data outputs a row respectively, which is the height of the binary tree.
Input sample 1
abcd00e00f00ig00h00 abd00e00cf00g00 0
Output sample 1
4 3
Use two int s maxi and max1 to record the overall maximum and the current maximum, respectively.
Wrong idea:
If bt reaches NULL, it indicates that it has traversed to the end of a branch. At this time, compare maxi and max1, update the value of maxi and make max1=0. Otherwise, make max1 + + and continue traversal.
The error is that max1=0 should not be allowed, because after retreating once, other nodes are based on this node path, which is equivalent to "standing on the shoulders of predecessors". If you empty all the previous paths because the current node has reached the end, and then traverse other branches from its parent, There will be a problem that nodes above the parent are not calculated into the total path!
Error code:
#include <iostream> using namespace std; typedef struct BNode { char data; struct BNode* lchild, *rchild; }*BTree, BNode; int max1 = 0, maxi = 0; void Create(BTree& bt) { char c; cin >> c; if (c == '0') bt = NULL; else { bt = new BNode; bt->data = c; Create(bt->lchild); Create(bt->rchild); } } void Create(BTree& bt,char c) { if (c == '0') bt = NULL; else { bt = new BNode; bt->data = c; Create(bt->lchild); Create(bt->rchild); } } void Traverse(BTree bt) { if (bt == NULL) { maxi = max1 > maxi ? max1 : maxi; max1 = 0; return; } else { max1++; Traverse(bt->lchild); Traverse(bt->rchild); } } int main() { while (1) { char c; cin >> c; if (c == '0') break; BTree b; Create(b, c); Traverse(b); cout << maxi << endl; maxi = 0; } return 0; }
Correct thinking:
The idea of setting maxi and max1 is correct. This calculation length is somewhat similar to the idea of DFS maze problem, that is, first start from the current node and go through all possible nodes, then go back and traverse other paths, compare the sizes of the two paths and take the maximum value. So we need to make max1 go back after each traversal -- (that is, go back to your own step and try another way).
It's really necessary to learn to draw inferences from one instance. If you can't learn DFS, your ideas will be limited to the maze problem!
The correct code is as follows:
#include <iostream> using namespace std; typedef struct BNode { char data; struct BNode* lchild, * rchild; }*BTree, BNode; int max1 = 0, maxi = 0;//max cannot be written. max is the name of the built-in function void Create(BTree& bt) { char c; cin >> c; if (c == '0') bt = NULL; else { bt = new BNode; bt->data = c; Create(bt->lchild); Create(bt->rchild); } } void Create(BTree& bt, char c) { if (c == '0') bt = NULL; else { bt = new BNode; bt->data = c; Create(bt->lchild); Create(bt->rchild); } } void Traverse(BTree bt) { if (bt) { max1++; Traverse(bt->lchild); Traverse(bt->rchild); maxi = max1 > maxi ? max1 : maxi;//Update the value of maxi max1--;//max1 returns to the previous position, assuming that this step has not been taken } } int main() { while (1) { char c; cin >> c; if (c == '0') break; BTree b; Create(b, c); Traverse(b); cout << maxi << endl; maxi = 0; max1 = 0; } return 0; }