文件: root - text - article - 2019 - 12 - count-number-of-unival-subtrees.txt.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 477 次阅读, 19773 次搜索 | 135 个单词
| 浏览 | 博客存档
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 477 次阅读, 19773 次搜索 | 135 个单词
| 浏览 | 博客存档
Hi, here's your problem today. This problem was recently asked by Microsoft:
A unival tree is a tree where all the nodes have the same value. Given a binary tree, return the number of unival subtrees in the tree.
For example, the following tree should return 5:
The 5 trees are:
- The three single '1' leaf nodes. (+3)
- The single '0' leaf node. (+1)
- The [1, 1, 1] tree at the bottom. (+1)
Here's a starting point:
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 477 次阅读, 19773 次搜索 | 135 个单词 A unival tree is a tree where all the nodes have the same value. Given a binary tree, return the number of unival subtrees in the tree.
For example, the following tree should return 5:
0
/ \
1 0
/ \
1 0
/ \
1 1
The 5 trees are:
- The three single '1' leaf nodes. (+3)
- The single '0' leaf node. (+1)
- The [1, 1, 1] tree at the bottom. (+1)
Here's a starting point:
class Node(object):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def count_unival_subtrees(root):
# Fill this in.
a = Node(0)
a.left = Node(1)
a.right = Node(0)
a.right.left = Node(1)
a.right.right = Node(0)
a.right.left.left = Node(1)
a.right.left.right = Node(1)
print count_unival_subtrees(a)
# 5
猜您喜欢...
- Algorithm Interview: Level Order Traversal of Binary Tree
- Daily Interview Problem: Contiguous Subarray with Maximum Sum
- Autorun.inf Virus Protection
- Daily Interview Problem: Jump to the End
- Daily Interview Problem: Minimum Removals for Valid Parenthesis
- Linode Support Ticket 10029540 - Other - Important Notice Regarding Ubuntu 17.10 Image
- [Daily Problem] Longest Increasing Subsequence
- Daily Interview Problem: Count Number of Unival Subtrees
- Algorithm Interview Question: Nth Fibonacci Number
- Algorithm Interview: No Adjacent Repeating Characters
©2006~2024 牛排过熟 - 0.00722 秒 - 7700.012 KB/s - 43 在线 内存: 517.62 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
牛排怎么做才好吃? | <meta name="机器人" content="不索引, 跟踪" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
评论 (0)
读写完全 - 一般 - 最小 - 表格 - 所有评论 - 统计
当前页暂时没有评论。