版权声明: 本文为博主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) - 题解的更多相关文章

  1. 剑指offer——面试题19:正则表达式匹配

    #include"iostream" using namespace std; bool MatchCore(char*str,char* pattern); bool Match ...

  2. 剑指Offer:面试题19——二叉树的镜像(java实现)

    问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...

  3. 剑指offer面试题19 二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像.  输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  4. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  5. 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 ...

  6. 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 ...

  7. <LeetCode OJ> 226. Invert Binary Tree

    226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...

  8. Python解Leetcode: 226. Invert Binary Tree

    leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...

  9. C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解

    剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...

随机推荐

  1. vs2013下配置x64版c++

    最近在ddctf的比赛遇到了x64版的逆向,一大堆寄存器调试的头昏,然后比赛结束后在自己电脑上配置下x64版的c++环境记录下: 首先我们需要新建项目不再废话,然后选择:debug->配置管理器 ...

  2. 数组遍历for forEach for..in for..of

    最开始接触的遍历for 通过下标获取数组每一项的值 ,,]; ;i<num.length;i++) { console.log(num[i]) } /*打印2 5 7*/ forEach遍历数组 ...

  3. 8080端口被System占用

    System是Windows页面内存管理进程,拥有0级优先权,没有它系统无法启动 就是说,System进程是无法关闭的,所以不要尝试去强行关闭,可能引起电脑异常查看是否是IIS占用的, 进入电脑控制面 ...

  4. The First of my text

    JavaScript 学习第一天 一.属性可以动态拓展 例如: var person = { name : "zs"; age : 21; } person.sex = " ...

  5. XIX Open Cup named after E.V. Pankratiev. GP of Poland(AMPPZ-2018)

    A. Drone With a Camera 三分套三分. #include<cstdio> #include<cmath> #include<algorithm> ...

  6. android BLE Peripheral 模拟 ibeacon 发出ble 广播

    Android对外模模式(peripheral)的支持: 从Android 5.0+开始才支持. api level >= 21 所以5.0 之前设备,是不能向外发送广播的. Android中心 ...

  7. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习8

    #include <iostream> #include <fstream> #include <cstdlib> const int SIZE=20; using ...

  8. data parameter is nil 异常处理

    似乎是NSData的问题,用排除法分析了一下 NSString *urlStr = [NSString stringWithFormat:@"%@/config/AppConfig.json ...

  9. Oracle expdp数据泵导出,并在文件上附加上日期格式

    一.导出操作的计算机要安装Oracle Client(建议管理员版本) 二.在服务端创建目录 create directory dpdir as '目录'; 三.给目录赋权限 grant read,w ...

  10. SDL 开发实战(七): 使用 SDL 实现 PCM播放器

    在上文,我们做了YUV播放器,这样我们就入门了SDL播放视频.下面我们来做一个PCM播放,即使用SDL播放PCM数据. 下面说明一下使用SDL播放PCM音频的基本流程,主要分为两大部分:初始化SDL. ...