文件: root - text - article - 2019 - 10 - invert-a-binary-tree.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | 英文 | 主页 | 类别: 计算机科学 | 619 次阅读, 21088 次搜索 | 164 个单词
| 浏览 | 博客存档
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | 英文 | 主页 | 类别: 计算机科学 | 619 次阅读, 21088 次搜索 | 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 次阅读, 21088 次搜索 | 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
猜您喜欢...
- Number of Ways to Climb Stairs
- Palindrome Integers
- Algorithm Interview: Maximum Path Sum in Binary Tree
- Algorithm Interview: Smallest Number that is not a Sum of a Subset of List
- Daily Interview Puzzle: Intersection of Linked Lists
- Daily Interview Problem: Group Words that are Anagrams
- Find Pythagorean Triplets
- Daily Interview Problem: Find the Number of Islands
- Algorithm Interview: Level Order Traversal of Binary Tree
- Daily Interview Problem: Get all Values at a Certain Height in a Binary Tree
©2006~2024 牛排过熟 - 0.00746 秒 - 2781.966 KB/s - 33 在线 内存: 494.35 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)
读写完全 - 一般 - 最小 - 表格 - 所有评论 - 统计
当前页暂时没有评论。