Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

单值二叉树 #145

Open
yankewei opened this issue May 24, 2022 · 1 comment
Open

单值二叉树 #145

yankewei opened this issue May 24, 2022 · 1 comment
Labels
广度优先搜索 题目包含广度优先搜索解法 题目类型为树结构 深度优先搜索 题目包含深度优先搜索解法 简单 题目难度为简单

Comments

@yankewei
Copy link
Owner

如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。

只有给定的树是单值二叉树时,才返回 true;否则返回 false。

示例 1:

image

输入:[1,1,1,1,1,null,1]
输出:true

示例 2:
image

输入:[2,2,2,5,2]
输出:false

提示:

  • 给定树的节点数范围是 [1, 100]。
  • 每个节点的值都是整数,范围为 [0, 99] 

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/univalued-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

@yankewei yankewei added 简单 题目难度为简单 广度优先搜索 题目包含广度优先搜索解法 深度优先搜索 题目包含深度优先搜索解法 题目类型为树结构 labels May 24, 2022
@yankewei
Copy link
Owner Author

深度优先搜索

先序遍历

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func isUnivalTree(root *TreeNode) bool {
    return isSameVal(root, root.Val)
}

func isSameVal(tree *TreeNode, val int) bool {
    if tree == nil {
        return true
    }
    
    if tree.Val != val {
        return false
    }

    return isSameVal(tree.Left, val) && isSameVal(tree.Right, val)
}

@yankewei yankewei changed the title 965. 单值二叉树 单值二叉树 May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
广度优先搜索 题目包含广度优先搜索解法 题目类型为树结构 深度优先搜索 题目包含深度优先搜索解法 简单 题目难度为简单
Projects
None yet
Development

No branches or pull requests

1 participant