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 does LeetCode find all the elements in two binary search trees

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

Share

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

This article is about how LeetCode finds all the elements in two binary search trees. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

One, two binary search trees for all the elements

1. Brief introduction of the problem.

Here are two binary search trees, root1 and root2.

Please return a list of all the integers in the "two trees" and sort them in ascending order.

2, example description

Input: root1 = [2jue 1pr 4], root2 = [1je 0pr 3]

Output: [0meme 1 pyrrine 1 pyrrine 2pime 3 pyrrine 4]

Example 2:

Input: root1 = [0jimi 10jue 10], root2 = [5meme 1je 7je 0je 2]

Output: [- 10pc0re0p0pl 1pyrrine 25pyrm 7pc10]

Example 3:

Input: root1 = [], root2 = [5Jing 1Jing 7JI 0jue 2]

Output: [0pyrrine 1 pyrrine 2pyrrine 5pr 7]

Example 4:

Input: root1 = [0mai Mei 10j 10], root2 = []

Output: [- 1010 01010]

Tip:

Each tree has a maximum of 5000 nodes.

The value of each node is between [- 10 ^ 5, 10 ^ 5].

3, the train of thought of solving the problem

Depth first search, array sort operation

4, problem solving procedure

Import java.util.ArrayList

Import java.util.Collections

Import java.util.List

Public class GetAllElementsTest3 {

Public static void main (String [] args) {

TreeNode T1 = new TreeNode (2)

TreeNode T12 = new TreeNode (1)

TreeNode T13 = new TreeNode (4)

TreeNode T2 = new TreeNode (1)

TreeNode T21 = new TreeNode (0)

TreeNode T22 = new TreeNode (3)

T1.left = T12

T1.right = T13

T2.left = T21

T2.right = T22

GetAllElements (T1, T2)

}

Public static List getAllElements (TreeNode root1, TreeNode root2) {

List list = new ArrayList ()

If (root1 = = null & & root2 = = null) {

Return list

}

List root1List = new ArrayList ()

List root2List = new ArrayList ()

DfsRoot1 (root1, root1List)

DfsRoot1 (root2, root2List)

Root1List.addAll (root2List)

Collections.sort (root1List)

Return root1List

}

Private static void dfsRoot1 (TreeNode root1, List root1List) {

If (root1 = = null) {

Return

}

If (root1.left! = null) {

DfsRoot1 (root1.left, root1List)

}

Root1List.add (root1.val)

If (root1.right! = null) {

DfsRoot1 (root1.right, root1List)

}

}

}

Thank you for reading! This is the end of the article on "how LeetCode finds all the elements in two binary search trees". I hope the above content can be of some help to you, so that 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.

Share To

Internet Technology

Wechat

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

12
Report