页面顶部 Top
文件:  root - text - article - 2019 - 10 - invert-a-binary-tree.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | 英文 | 主页 | 类别: 计算机科学 | 329 次阅读, 14580 次搜索 | 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:


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 Problem, Data Structures and Algorithms, | 英文 | 主页 | 类别: 计算机科学 | 329 次阅读, 14580 次搜索 | 164 个单词 定阅此目录的博客

猜您喜欢...

  1. [Daily Problem] Remove k-th Last Element From Linked List
  2. Sorting a list with 3 unique numbers
  3. Daily Interview Problem: Find the Number of Islands
  4. Daily Interview Problem:Create a balanced binary search tree
  5. ImageMagick Vulnerabilities -  CVE-2016–3714
  6. Most Frequent Subtree Sum
  7. Staying on a Chess Board
  8. Daily Interview Problem: Contiguous Subarray with Maximum Sum
  9. Algorithm Interview: Smallest Number that is not a Sum of a Subset of List
  10. Algorithm Interview Question: Max and Min with Limited Comparisons

评论 (0)

    当前页暂时没有评论。


最后更新: October 30 2020 14:21:12 | RSS Subscription
牛排怎么做才好吃? | <meta name="机器人" content="索引, 跟踪">