【一天一道LeetCode】#226. Invert Binary Tree
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
来源:https://leetcode.com/problems/invert-binary-tree/
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
(二)解题
题目大意:将一个二叉树倒置。即每个节点的左右节点交换位置。
解题思路:采用前序遍历,从根节点开始,对每个节点进行左右节点交换位置。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
dfsInvertTree(root);
return root;
}
//前序遍历
void preOrderInvertTree(TreeNode* root){
if(root==NULL) return;
//交换左右节点
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
if(root->left != NULL) dfsInvertTree(root->left);
if(root->right != NULL) dfsInvertTree(root->right);
}
};
【一天一道LeetCode】#226. Invert Binary Tree的更多相关文章
- LeetCode 226. Invert Binary Tree (反转二叉树)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- Leetcode 226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 class Solution(object): ...
- Java for LeetCode 226 Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...
- (easy)LeetCode 226.Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- Java [Leetcode 226]Invert Binary Tree
题目描述: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 解题思路: 我只想说递归大法好. ...
- Leetcode 226 Invert Binary Tree python
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- leetcode 226 Invert Binary Tree 翻转二叉树
大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Tri ...
- Leetcode 226. Invert Binary Tree(easy)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- LeetCode 226 Invert Binary Tree 解题报告
题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...
- LeetCode (226):Invert Binary Tree 递归实现
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
随机推荐
- Machine Learning From Scratch-从头开始机器学习
Python implementations of some of the fundamental Machine Learning models and algorithms from scratc ...
- Android开发Java基础之Java语言基础(1)
Java中的基本数据类型 整数类型 整数类型用来存储整数数值,既没有小数部分的数值.可以是正数,也可以是负数.整数类型在Java程序中有三种表现形式,分别是十进制,八进制,十六进制. 整型数据根据它所 ...
- JS文件中获取contextPath的方法
function getContextPath() { var pathName = document.location.pathname; var index = pathName.su ...
- sublime text3中设置Emmet输入标签自动闭合
项目后端前一段时间从C#转成了JAVA,在开发的过程中,由于HTML对标签的语法很宽松,比如这样:<img src="" alt="">在标签的结尾 ...
- 轻松理解AOP问题
先说一个Spring是什么吧,大家都是它是一个框架,但框架这个词对新手有点抽象,以致于越解释越模糊,不过它确实是个框架的,但那是从功能的角度来定义的,从本质意义上来讲,Spring是一个库,一个Jav ...
- 解决 Popup 位置不随窗口移动更新的问题
Popup弹出后,因业务需求设置了StaysOpen=true后,移动窗口位置或者改变窗口大小,Popup的位置不会更新. 如何更新位置? 获取当前Popup的Target绑定UserControl所 ...
- docker管理工具
Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控制台操作.Swarm集群和服 ...
- 条件语句,while循环语句:完整的温度转换程序
while True: a = int(input('摄氏温度换为华氏温度请按 1\n华氏温度转为摄氏温度请按 2\n退出请按 3\n')) if a==1: c = float(input('请输入 ...
- /usr,/usr/local/ 还是 /opt ?
Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有益的(好吧处女座表示完全不能接受不正确的路径选择,看着会不舒服的……) /usr:系统级的目录,可以理解为C:/Windows/, ...
- How To Automate Disconnection of Idle Sessions
***Checked for relevance on 30-Apr-2012*** goal: How to automate disconnection of idle sessions fact ...