File: root - text - article - 2019 - 10 - invert-a-binary-tree.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | English | Home Page | Category: Computing | 620 Views, 21132 Search Bots | 164 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | English | Home Page | Category: Computing | 620 Views, 21132 Search Bots | 164 Words
| Browse | Archive
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:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | English | Home Page | Cateogry: Computing | 620 Views, 21132 Search Bots | 164 Words 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
Related Articles
- Daily Interview Problem: Word Ordering in a Different Alphabetical Order
- Fix Brackets
- Floor and Ceiling of a Binary Search Tree
- Daily Interview Problem: Reconstrunct Binary Tree from Preorder and Inorder Traversals
- Daily Interview Problem: Get all Values at a Certain Height in a Binary Tree
- Staying on a Chess Board
- Daily Interview Problem:Create a balanced binary search tree
- Top K Frequent words
- Linode Support Ticket 10029540 - Other - Important Notice Regarding Ubuntu 17.10 Image
- [Daily Problem] Add two numbers as a linked list
©2006~2024 SteakOverCooked - 0.007 Seconds(s) - 7769.816 KB/s - 43 Online Memory: 516.9 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" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !