leetcode 226. Invert Binary Tree
Invert a binary tree.
4/ \2 7/ \ / \ 1 3 6 9
to
4/ \7 2/ \ / \ 9 6 3 1
解法1:
本质是输的先序遍历
# 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"""# invert self.left# invert self.rightif not root: return Noneroot.left, root.right = root.right, root.leftself.invertTree(root.left)self.invertTree(root.right)return root
解法2,上述DFS的迭代解法:
# 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 not root: return Nonestack = [root]while stack:node = stack.pop()node.left, node.right = node.right, node.leftif node.right:stack.append(node.right)if node.left:stack.append(node.left)return root
解法3,使用BFS:
# 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 not root: return Nonenodes = [root]while nodes:nodes2 = []for node in nodes:node.left, node.right = node.right, node.leftif node.left: nodes2.append(node.left)if node.right: nodes2.append(node.right)nodes = nodes2return root
转载于:https://www.cnblogs.com/bonelee/p/8563696.html
leetcode 226. Invert Binary Tree相关推荐
- Leetcode 226: Invert Binary Tree
问题描述: Given the root of a binary tree, invert the tree, and return its root. 将二叉树镜像 思路: 发现子问题:将给定二叉树 ...
- LeetCode #226 - Invert Binary Tree - Easy
Problem Invert a binary tree. Example 4/ \2 7/ \ / \ 1 3 6 9->4/ \7 2/ \ / \ 9 6 3 1 Algorithm 整理 ...
- LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法
题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...
- 226. Invert Binary Tree 1
题目链接:Invert Binary Tree 思路: 如果需要反转一个二叉树,那么我们需要遍历整个树的所有节点. 如果想遍历所有的节点,我们可以用Depth First Search(DFS)或者B ...
- leetcode python3 简单题226. Invert Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十六题 (1)题目 英文: Invert a binary tree. 中文 ...
- [LeetCode][JavaScript]Invert Binary Tree 反转二叉树
反转二叉树 其实我从没有想到前端面试会问到这个问题,题目来源于google的面试 Google: 90% of our engineers use the software you wrote (Ho ...
- Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树所有左右子数 ...
- Leetcode-标签为Tree 226. Invert Binary Tree
原题 Invert a binary tree. 4/ \ 2 7/ \ / \ 1 3 6 9 to 4/ \ 7 2/ \ / \ 9 6 3 1 代码分析 反转二叉树 代码实现 public T ...
- 226. Invert Binary Tree
做法就是: 如果root是null,返回 swap左右两树(用tmp作为第三个容器),然后对左右子树递归 返回root 1 public TreeNode invertTree(TreeNode ro ...
最新文章
- C语言比较好的风格梳理
- 十大经典排序算法Python版实现(附动图演示)
- [2013.8.29]对于多线程编程的几点个人见解
- 在ECS系统中使用IJobChunk作业
- win10 python3.5.2环境下 安装xgboost
- windows server 2008中IIS7的功能模塊
- [转]网络爬虫(一):抓取网页的含义和URL基本构成
- javaWeb—9.Git
- uml通信图画法_UML9种图的画法
- html网站运行天数代码,给网站加上运行时间天数统计代码
- python爬取雪球网交易数据
- 数据结构-----------------------哈希表(最通俗易懂的文章)
- php抓取页面方法汇总
- Number Game(数字游戏)
- 汗,Ackerman函数......
- 教你在微信拼接长图片
- 【转】配置Symbian模拟器支持模拟MMC存储卡
- axure动态面板滑动效果
- Linux FHS结构
- TI DSP 28335 看门狗(WatchDog)及通过看门狗实现中断
热门文章
- linux 批量替换所有文件中包含的字符串
- selenium webdriver中执行js(java)
- Redis Cluster 介绍与搭建
- 路径搜索算法 python实现_A*算法在栅格地图上的路径搜索(python实现)
- linux zsh命令行vim命令补齐,Linux使用zsh提高效率的5条建议
- python中怎么绘制柱状簇_用Python绘制簇的质心
- python【力扣LeetCode算法题库】42-接雨水(双指针法)
- git和github的关系
- DL之决策树(Decision tree)
- 【深度学习笔记】SIFT特征和SURF特征比较