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