[python]226. Invert Binary Tree; 60ms
来源:互联网 发布:网络赌徒的下场 编辑:程序博客网 时间:2024/05/19 09:03
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 1Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def invertTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if root == None :
return root
stack = [root]
while stack:
s = stack.pop()
# 不单写swap函数可以提高效率
tmp = s.left
s.left = s.right
s.right = tmp
if s.left:
stack.append(s.left)
if s.right:
stack.append(s.right)
return root
0 0
- [python]226. Invert Binary Tree; 60ms
- 226. Invert Binary Tree [easy] (Python)
- [leetcode: Python]226. Invert Binary Tree
- LeetCode 226. Invert Binary Tree(Python)
- 226.Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- 缓存资源
- 带空格字符串读取
- [总结]FFMPEG视音频编解码零基础学习方法
- JAVASE总结--冒泡排序
- 为代码添加权限以及使用时的权限检查
- [python]226. Invert Binary Tree; 60ms
- View 滑动的实现
- 搭建heron环境(一)安装JDK
- 从键盘输入一个身份证号,获得该身份证主人年龄
- java虚拟机老年代垃圾收集器
- Handler机制
- 虚函数
- CMS(Concurrent Mark Sweep,并行标记-清除)收集器
- G1垃圾收集器