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 parse a single-valued binary tree

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

Share

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

Today, I will talk to you about how to analyze the single-valued binary tree, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Brief introduction of the problem.

If each node of the binary tree has the same value, then the binary tree is a single-valued binary tree.

True; is returned only if the given tree is a single-valued binary tree, otherwise false is returned.

2, example

Input: [1Jing 1Jing 1JI Jing 1JI] output: true

3, the train of thought of solving the problem

Using pre-order traversal and queue method to solve the problem

4, problem solving procedure

Import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Queue

Public class IsUnivalTreeTest {public static void main (String [] args) {TreeNode T1 = new TreeNode (1); TreeNode T2 = new TreeNode (1); TreeNode T3 = new TreeNode (1); TreeNode T4 = new TreeNode (1); TreeNode T5 = new TreeNode (1); TreeNode T6 = new TreeNode (1); t1.left = T2; t1.right = T3; t2.left = T4 T2.right = T5; t3.right = T6; boolean univalTree = isUnivalTree2 (T1); System.out.println ("univalTree =" + univalTree)

}

Public static boolean isUnivalTree (TreeNode root) {if (root = = null) {return true;} Queue queue = new LinkedList (); queue.add (root); List list = new ArrayList (); while (! queue.isEmpty ()) {int size = queue.size (); for (int I = 0; I < size) ITunes +) {TreeNode treeNode = queue.poll (); list.add (treeNode.val); if (treeNode.left! = null) {queue.add (treeNode.left);} if (treeNode.right! = null) {queue.add (treeNode.right) } Integer val = list.get (0); for (int num: list) {if (num! = val) {return false;}} return true;}

Public static boolean isUnivalTree2 (TreeNode root) {if (root = = null) {return true;} if (root.left! = null & & root.val! = root.left.val) {return false;} if (root.right! = null & & root.val! = root.right.val) {return false } return isUnivalTree (root.left) & & isUnivalTree (root.right);}}

5. Picture version of the problem solving program.

After reading the above, do you have any further understanding of how to analyze a single-valued binary tree? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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