Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to analyze cyclic traversing binary Tree in python

Shulou Source: shulou.com Published: 2022-06-01 14:11:36 11月17日 Update

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

Tags: Loop help analysis clarity content function for this article novice more knowledge industry information information channel process problem demand channel enter one recursive Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology Docker Shulou Information Shulou Tech Info