Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to print binary tree from top to bottom with java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "java how to print binary tree from top to bottom". In daily operation, I believe many people have doubts about how to print binary tree from top to bottom with java. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how to print binary tree from top to bottom with java". Next, please follow the editor to study!

Preface

The binary tree is printed from top to bottom, and queues are used here, so let's talk about Java queues first.

Queue

Create a queue

Queue queue = new LinkedList ()

Add element

Queue.offer ("a")

Out of queue

/ / return the first element and delete it in the queue

Queue.poll ()

/ / returns the element of the queue header. If the queue is empty, a NoSuchElementException exception is thrown.

Queue.element ()

/ / returns the element of the queue header, or null if the queue is empty

Queue.peek ()

These are the main methods that may be used. Let's take a look at the title.

Title

Each node of the binary tree is printed from top to bottom, and the nodes on the same layer print from left to right.

Analysis.

Print a binary tree. If you traverse the print directly, the root node-> left node-> right node will be printed first. If you want to print according to the hierarchy, you can do it according to the queue, adding the nodes to the queue from the root node in turn, and then taking them out of the queue to achieve the purpose of hierarchical printing.

Solution ArrayList list=new ArrayList ()

If (root==null) {

Return list

}

Queue queue=new LinkedList ()

Queue.offer (root)

While (! queue.isEmpty ()) {

TreeNode temp=queue.poll ()

List.add (temp.val)

If (temp. Leftwatches null) {

Queue.offer (temp.left)

}

If (temp. Rightweights null) {

Queue.offer (temp.right)

}

}

Return list

test

Main method

Public static void main (String [] args) {

TreeNode root = new TreeNode (1)

Root.left=new TreeNode (2)

Root.right=new TreeNode (3)

Root.left.left=new TreeNode (4)

Root.right.left=new TreeNode (5)

Root.left.left.left=new TreeNode (6)

TreeOperation.show (root)

Solution solution= new Solution ()

ArrayList list=solution.PrintFromTopToBottom (root)

For (int iTuno Bandi)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report