Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to flip python binary tree

Shulou Source: shulou.com Published: 2022-06-01 14:13:01 10月02日 Update

This article shows you how to flip the python binary tree, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Flip a binary tree.

Example:

Enter:

4 / 2 7 / 1 3 6 9

Output:

4 /\ 7 2 /\ 9 6 3 1

The problem in the previous issue is: 160, using queues to implement the stack

1class MyStack {

2 private Queue que

three

4 public MyStack () {

5 que = new LinkedList ()

6}

seven

8 public void push (int x) {

9 que.offer (x)

10 for (int I = 0; I < que.size ()-1; + + I) {

11 que.offer (que.poll ())

12}

13}

fourteen

15 public int pop () {

16 return que.poll ()

17}

eighteen

19 public int top () {

20 return que.peek ()

21}

twenty-two

23 public boolean empty () {

24 return que.isEmpty ()

25}

26}

The offer method stores x at the end of the que. Because the queue is first-in, first-out, and the stack is first-in, first-out, so here push will first store x to the end of the queue, and then put the previous one out of the queue and then rejoin the queue.

The above content is how to flip the python binary tree. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Queue advanced content skills knowledge concise concise that is articles methods times more examples articles industry information channels questions channels one highlight Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi Xiaomi vpn Linux