File: root - text - article - 2019 - 12 - full-binary-tree.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 679 Views, 18695 Search Bots | 171 Words
| Browse | Archive
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 679 Views, 18695 Search Bots | 171 Words
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Hi, here's your problem today. This problem was recently asked by Google:
Given a binary tree, remove the nodes in which there is only 1 child, so that the binary tree is a full binary tree.
So leaf nodes with no children should be kept, and nodes with 2 children should be kept as well.
Here's a starting point:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 679 Views, 18695 Search Bots | 171 Words Given a binary tree, remove the nodes in which there is only 1 child, so that the binary tree is a full binary tree.
So leaf nodes with no children should be kept, and nodes with 2 children should be kept as well.
Here's a starting point:
from collections import deque
class Node(object):
def __init__(self, value, left=None, right=None):
self.left = left
self.right = right
self.value = value
def __str__(self):
q = deque()
q.append(self)
result = ''
while len(q):
num = len(q)
while num > 0:
n = q.popleft()
result += str(n.value)
if n.left:
q.append(n.left)
if n.right:
q.append(n.right)
num = num - 1
if len(q):
result += "\n"
return result
def fullBinaryTree(node):
# Fill this in.
# Given this tree:
# 1
# / \
# 2 3
# / / \
# 0 9 4
# We want a tree like:
# 1
# / \
# 0 3
# / \
# 9 4
tree = Node(1)
tree.left = Node(2)
tree.right = Node(3)
tree.right.right = Node(4)
tree.right.left = Node(9)
tree.left.left = Node(0)
print fullBinaryTree(tree)
# 1
# 03
# 94
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Related Articles
- Algorithm Interview: Make the Largest Number
- Two Tricks of Delphi
- Daily Interview Problem: Deepest Node in a Binary Tree
- Daily Interview Problem: Merge Overlapping Intervals
- Daily Interview Problem: Maximum Profit From Stocks
- Daily Interview Problem: Room scheduling
- Algorithm Interview Question: Nth Fibonacci Number
- YES!!
- Daily Interview Problem: Distribute Bonuses
- Algorithm Interview: Permutations of numbers
©2006~2024 SteakOverCooked - 0.01184 Seconds(s) - 4643.784 KB/s - 41 Online
Memory: 516.87 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
![](/images/top.jpg)
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !