文件: root - text - article - 2019 - 10 - invert-a-binary-tree.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | 英文 | 主页 | 类别: 计算机科学 | 619 次阅读, 21093 次搜索 | 164 个单词
| 浏览 | 博客存档
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | 英文 | 主页 | 类别: 计算机科学 | 619 次阅读, 21093 次搜索 | 164 个单词
| 浏览 | 博客存档
Hi, here's your problem today. This problem was recently asked by Twitter:
You are given the root of a binary tree. Invert the binary tree in place. That is, all left children should become right children, and all right children should become left children.
Example:
The inverted version of this tree is as follows:
Here is the function signature:
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | 英文 | 主页 | 类别: 计算机科学 | 619 次阅读, 21093 次搜索 | 164 个单词 You are given the root of a binary tree. Invert the binary tree in place. That is, all left children should become right children, and all right children should become left children.
Example:
a
/ \
b c
/ \ /
d e f
The inverted version of this tree is as follows:
a
/ \
c b
\ / \
f e d
Here is the function signature:
class Node:
def __init__(self, value):
self.left = None
self.right = None
self.value = value
def preorder(self):
print self.value,
if self.left: self.left.preorder()
if self.right: self.right.preorder()
def invert(node):
# Fill this in.
root = Node('a')
root.left = Node('b')
root.right = Node('c')
root.left.left = Node('d')
root.left.right = Node('e')
root.right.left = Node('f')
root.preorder()
# a b d e c f
print "\n"
invert(root)
root.preorder()
# a c f b e d
猜您喜欢...
- Daily Interview Problem: Deepest Node in a Binary Tree
- Daily Interview Problem: Circle of Chained Words
- Algorithm Interview: Longest Consecutive Sequence
- Daily Interview Problem: Full Binary Tree
- Daily Interview Puzzle: Minimum Size Subarray Sum
- Design Tic-Tac-Toe
- Spectrum Master
- Daily Interview Problem: Reconstrunct Binary Tree from Preorder and Inorder Traversals
- Daily Interview Problem: Arithmetic Binary Tree
- Reverse a Linked List
©2006~2024 牛排过熟 - 0.00707 秒 - 2678.567 KB/s - 29 在线 内存: 492.41 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)
读写完全 - 一般 - 最小 - 表格 - 所有评论 - 统计
当前页暂时没有评论。