python--leetcode226. Invert Binary Tree
来源:互联网 发布:tcp通信安卓app源码 编辑:程序博客网 时间:2024/05/16 11:55
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 = Noneclass Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if root: def invert(root): temp=root.left root.left=root.right root.right=temp if root.left: invert(root.left) if root.right: invert(root.right) invert(root) return root
更快捷解法:
def invertTree(self, root): if root: root.left, root.right = self.invertTree(root.right), self.invertTree(root.left) return root
阅读全文
1 0
- python--leetcode226. Invert Binary Tree
- LeetCode226:Invert Binary Tree
- Leetcode226 Invert Binary Tree
- leetcode226 Invert Binary Tree
- LeetCode226. Invert Binary Tree
- leetcode226: Invert Binary Tree
- leetcode226. Invert Binary Tree
- leetcode226. Invert Binary Tree
- LeetCode226——Invert Binary Tree
- 剑指offer 面试题19:二叉树的镜像(leetcode226. Invert Binary Tree) 题解
- 226. Invert Binary Tree [easy] (Python)
- [python]226. Invert Binary Tree; 60ms
- [leetcode: Python]226. Invert Binary Tree
- LeetCode 226. Invert Binary Tree(Python)
- [leetcode] Invert Binary Tree
- [LeetCode]Invert Binary Tree
- Invert Binary Tree
- leetcode:Invert Binary Tree
- 解决DLL文件丢失
- linux自动安装脚本
- 第一篇 理解PHP设计模式
- Android 开发使用String 类替换Html中字段问题
- 数据库三大范式以及各种键值
- python--leetcode226. Invert Binary Tree
- linux中设定系统延时任务及定时任务
- 老鼠试药问题
- 前端学习第六弹:制作一个简易导航
- [JZOJ2393]【ZJOI2011】营救皮卡丘
- shiro cookie
- tensorflow 学习笔记之 变量的一些操作
- HDU
- 第10周项目实践 线索二叉树的建立及遍历