Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to make a single-valued binary tree

Shulou Source: shulou.com Published: 2022-06-01 14:07:11 10月17日 Update

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!

Tags: Content methods more examples problems input output same helpless for this reason only for this skills articles experience nodes industries information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology Huawei Apple MariaDB