How to analyze cyclic traversing binary Tree in python
Python in how to analyze the cycle through the binary tree, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Preorder traversal
Struct Node {Node*left; Node*right; int data; Node () {func;}}; Node* create (Node*p, int depth) {if (p & & depth) {p-> left = new Node; p-> right = new Node; p-> data = depth; create (p-> left, depth-1) Create (p-> right, depth-1);} if (! depth) {p-> left = nullptr; p-> right = nullptr; p-> data = depth;} return p;} void print1 (Node*p) {if (p) {cout data left) Print1 (p-> right);} void print2 (Node*head) / / uses stack to simulate the function call process to traverse {stack s; Node*p = head; {while (p) {s.push (p); cout data left } while (! s.empty ()) {Node*pp = s.top (); if (pp- > right & & pp! = head) {cout right- > data right While (p) {s.push (p); cout data left;} while (! s.empty ()) {Node*pp = s.top () If (pp- > right & & pp! = head) {cout right- > data data = 10; head- > left- > data = 6; head- > right- > data = 14; head- > left- > left- > data = 4; head- > left- > right- > data = 8 Head- > right- > left- > data = 12; head- > right- > right- > data = 16; print1 (head); / / Recursive traversal cout data right; while (p) {s.push (p); p = p-> left } while (! s.empty ()) {Node*pp = s.top (); cout data right&& pp! = head) {cout right- > data left } while (! s.empty ()) {Node*pp = s.top () If (pp- > right & & pp! = head) {cout right- > data left } while (! s.empty ()) {Node*pp = s.top (); if (pp- > right&& pp! = head) {cout right- > data