C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址
http://blog.csdn.net/lzuacm。
C#版 - 226. Invert Binary Tree - 题解
在线提交: https://leetcode.com/problems/invert-binary-tree/
或 http://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171
题目描述
Invert a binary tree.
Example:
Input:
4
/ \
2 7
/ \ / \
1 3 6 9
Output:
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 f*** off.
Difficulty:
Easy
Total Accepted: 238.9K
Total Submissions: 443.1K
Related Topics Tree
思路:
交换左右子树的根节点,再递归地交换两棵子树的叶节点即可。当原树为null时,直接返回null~
已AC代码:
// Definition for a binary tree node.
//public class TreeNode
//{
// public int val;
// public TreeNode left;
// public TreeNode right;
// public TreeNode(int x) { val = x; }
//}
public class Solution
{
public TreeNode InvertTree(TreeNode root)
{
if(root == null)
return null;
TreeNode p;
p = root.left;
root.left = root.right;
root.right = p;
InvertTree(root.left);
InvertTree(root.right);
return root;
}
}
Rank:
You are here!
Your runtime beats 95.68 %
of csharp submissions.
C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解的更多相关文章
- 剑指offer——面试题19:正则表达式匹配
#include"iostream" using namespace std; bool MatchCore(char*str,char* pattern); bool Match ...
- 剑指Offer:面试题19——二叉树的镜像(java实现)
问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...
- 剑指offer面试题19 二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 226. Invert Binary Tree(C++)
226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- <LeetCode OJ> 226. Invert Binary Tree
226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...
- Python解Leetcode: 226. Invert Binary Tree
leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...
- C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解
剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...
随机推荐
- python3控制语句---选择结构语句
python中的控制语句主要有if.if--else.if--slif--else.pass语句.其实python的控制语句与其他语言的控制语句工作原理基本一样.控制语句可以分为选择结构语句和循环结构 ...
- Jmeter选项含义
最近接了组里压测的任务,开始仔细钻研Jmeter了.之前也压过,但每次RD问压测的指标等问题,感觉都很懵不知道该怎么回答.借这个机会一鼓作气搞明白吧! Jmeter安装插件 有个插件叫jp@gc St ...
- Python-模块,以及使用文本中的数据
模块导入: from math import pi as math_pi print math_pi #相当于把pi取了个别名 # -*- coding: cp936 -*-from rand ...
- 为什么目前无法再docker for windows中调用GPU
本随笔记载与2019年1月23日,若随着技术发展,本随笔记录的困难被攻克也是可能的. 参考(https://www.reddit.com/r/docker/comments/86vzna/gpu_ac ...
- mysql数据表增删改查
http://www.runoob.com/mysql/mysql-tutorial.html 一.MySQL 创建数据表 创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以 ...
- ubuntu 安装lnmp、swoole、redis
1.安装lnmp (此处也可用于centos) 登陆服务器后 cd /var screen -S lnmp wget http://soft.vpser.net/lnmp/lnmp1.5.tar.g ...
- vs中 VMDebugger未能加载导致异常
,纠结了许久的一个问题,终于找到了解决 vs中 VMDebugger未能加载导致异常 错误号:80004005 搜了好多,没有一个给出完美的答案. 解决办法:工具->导入和导出设置,重置一下 ...
- redis的过期策略都有哪些?
1.面试题 redis的过期策略都有哪些?内存淘汰机制都有哪些?手写一下LRU代码实现? 2.面试官心里分析 1)老师啊,我往redis里写的数据怎么没了? 之前有同学问过我,说我们生产环境的redi ...
- IndentityServer4
官网: https://identityserver4.readthedocs.io/en/latest/index.html 比较好的中文博客: 晓晨Master: https://www.cnbl ...
- React修改state(非redux)中数组和对象里边的某一个属性的值
在使用React时,会经常需要处理state里边设置的初始值以达到我们的实际需求,比如从接口获取到列表数据后要赋值给定义的列表初始值,然后数据驱动view视图进而呈现在我们眼前,这种最简单的赋值方式实 ...