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

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

Share

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

How to carry out single-valued binary tree, I believe that many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Single-valued binary tree

If each node of a 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.

Example 1:

Input: [1Jing 1JI 1 "

Output: true

Example 2:

Input: [2, 2, 2, 2, 2, 2, 5, 5, 2.

Output: false

Type TreeNode struct {Val int Left * TreeNode Right * TreeNode} func isUnivalTree (root * TreeNode) bool {if root = = nil {return true} stack: = [] * TreeNode {root} var v = root.Val for len (stack) > 0 {size: = len (stack) for I: = 0; I < size ITunes + {node: = stack [I] if node.Val! = v {return false} if node.Left! = nil {stack = append (stack, node.Left)} if node.Right! = nil {stack = append (stack) Node.Right)}} stack = stack [size:]} return true read the above content Have you mastered how to make a single-valued binary tree? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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