In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to expand the binary tree into a linked list, the content is very detailed, interested friends can refer to, hope to be helpful to you.
The binary tree is expanded into linked table 1, a brief description of the problem
Given a binary tree, expand it into a single linked list in place.
2, an example is briefly described, for example, a given binary tree
one
/\
2 5
/\\
3 4 6
Expand it into:
one
\
two
\
three
\
four
\
five
\
six
3, the train of thought of solving the problem
Rebuild the binary tree
4, problem solving program import java.util.LinkedList
Import java.util.List
Public class FlattenTest2 {
Public static void main (String [] args) {
TreeNode T1 = new TreeNode (1)
TreeNode T2 = new TreeNode (2)
TreeNode T3 = new TreeNode (5)
TreeNode T4 = new TreeNode (3)
TreeNode T5 = new TreeNode (4)
TreeNode T6 = new TreeNode (6)
T1.left = T2
T1.right = T3
T2.left = T4
T2.right = T5
T3.right = T6
Flatten (T1)
System.out.println ("T1 =" + T1)
}
Public static void flatten (TreeNode root) {
If (root = = null) {
Return
}
LinkedList list = new LinkedList ()
Dfs (root, list)
TreeNode head = list.removeFirst ()
Head.left = null
While (list.size () > 0) {
TreeNode tempNode = list.removeFirst ()
TempNode.left = null
Head.right = tempNode
Head = head.right
}
}
Private static void dfs (TreeNode root, List list) {
If (root = = null) {
Return
}
List.add (root)
If (root.left! = null) {
Dfs (root.left, list)
}
If (root.right! = null) {
Dfs (root.right, list)
}
}
}
On how to achieve binary tree expansion into a linked list is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.