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 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.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==nullptr||(root->left==nullptr&&root->right==nullptr)){
return root;
}
else if(root->left==nullptr){
root->left=invertTree(root->right);
root->right=nullptr;
return root;
}
else if(root->right==nullptr){
root->right=invertTree(root->left);
root->left=nullptr;
return root;
}
TreeNode* p=root->left;
root->left=invertTree(root->right);
root->right=invertTree(p);
return root;
}
};
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 ...
- 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 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 Tri ...
- LeetCode 226 Invert Binary Tree 解题报告
题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...
- LeetCode 226 Invert Binary Tree(转换二叉树)
翻译 将下图中上面的二叉树转换为以下的形式.详细为每一个左孩子节点和右孩子节点互换位置. 原文 如上图 分析 每次关于树的题目出错都在于边界条件上--所以这次细致多想了一遍: void swapNod ...
随机推荐
- Huffman编码(Huffman树)
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "Huffman编码(Huffman树)" 的idea 并用源代码加以实现: 0.2) ...
- iphone开发技术要学习的内容
一.iOS基础 1 开发环境搭建以及IOS组件.框架的概要介绍. 2 mac操作系统与iOS操作系统 3 xcode IDE开发环境的初始 二.C语言基础 1数据类型.表达式与控制流程语句 2数组.函 ...
- android-support-v4.jar异常解决方法
1.当一个项目引入其他library项目时,会出现android-support-v4.jar冲突问题: 解决:将library项目中的android-support-v4.jar更新到最新,方法右键 ...
- jQuery动态加载JS以减少服务器压力
如果您要创建一个web2.0的应用程序,那么你的网页会包括大量的JavaScript文件,这些可能会拖慢您的网页.因此,动态加载JavaScript代码到您的网页是一个好主意,即只有当实用他们的时候加 ...
- - WebStorm 转载【干货技术贴】之-mac下如何安装WebStorm + 破解
写在前面 之前公司不忙的时候,用闲暇功夫想学习React-Native 苦于找不到一款好的代码编辑器,在广泛搜索以后,发现最适合的就是网页代码编辑器WebStrom,所以就尝试安装和破解,下面我将自己 ...
- PHPstudy如何在本地搭建多站点
参考地址: http://jingyan.baidu.com/article/e52e36154227ef40c70c5147.html
- Node-Webkit - package.json - 参数设置
必填: main :(string)APP的主入口,指定一个html文件,如:main:"index.htm". name :(string)APP的名称,必须具有唯一性. 例子: ...
- Gaby Ivanushka(快排)
Gaby Ivanushka Once upon a time there lived a tsar that has a daughter — Beautiful Vasilisa. There w ...
- 2.二级接口ListableBeanFactory
这个随笔主要讲的是ListableBeanFactory package org.springframework.beans.factory; import java.lang.annotation. ...
- nginx日志自动切分
#!/bin/bash NGINX_LOG_PATH=/data/nginx-/logs # 昨天 YESTERDAY=$(date -d "yesterday" +%Y-%m-% ...