1 题目

Invert a binary tree.


4
/ \
2 7
/ \ / \
1 3 6 9

to

     4
/ \
7 2
/ \ / \
9 6 3 1

接口: public TreeNode invertTree(TreeNode root)

2 思路

反转一颗二叉树。

可以用递归和非递归两种方法来解。

  • 递归的方法,写法非常简洁,五行代码搞定,交换当前左右节点,并直接调用递归即可。
  • 非递归的方法,参考树的层序遍历,借助Queue来辅助,先把根节点排入队列中,然后从队中取出来,交换其左右节点,如果存在则分别将左右节点在排入队列中,以此类推直到队列中木有节点了停止循环,返回root即可。

复杂度:两种方法的时间和空间复杂度一样,Time O(n);Space O(n)

3 代码

  • 思路1:
        public TreeNode invertTree(TreeNode root) {
if (root == null)
return root;
TreeNode tmp = root.left;
root.left = invertTree(root.right);
root.right = invertTree(tmp);
return root;
}

4 总结

考察二叉树的遍历。

非递归的实现,复习。请实现非递归。

5 参考

lc面试准备:Invert Binary Tree的更多相关文章

  1. 【07_226】Invert Binary Tree

    Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy Invert a binary ...

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

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

  4. C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...

  5. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  6. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

  7. 【Invert Binary Tree】cpp

    题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...

  8. <LeetCode OJ> 226. Invert Binary Tree

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

  9. LeetCode_226. Invert Binary Tree

    226. Invert Binary Tree Easy Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: ...

随机推荐

  1. mac os 终端提示 you have new mail

    这里的信息可能是由于所做的什么操作触发了发邮件的事件, 系统发送的邮件提醒. 我遇到的原因是由于运行 cron , 由于权限所导致了发邮件的事件提醒. Last login: Tue Apr 26 0 ...

  2. jwplayer 源代码重新编译

    由于原来下载的jwplaery播放器中带有官方的播放统计信息,需要从官方加载统计js脚本,影响播放器加载速度,因此从官方github站点上下载播放器进行重新编译操作,现记录过程如下. 下载最新的jwp ...

  3. 在iis中mantisbt配置过程

    最近需要安装个mantisbt,由于不想再安装个apache服务器,因此直接使用iis作为php解析服务器.同时为了方便管理安装包,将php安装包和扩展包能够独立存放在D:\Program Files ...

  4. U3D C#脚本的生命周期

    MonoBehaviour是每个脚本的基类. 每个Javascript脚本自动继承MonoBehaviour,使用C#或Boo时,需要显式继承MonoBehaviour. 一开始实例化,直到结束实例被 ...

  5. [Jquery] jQuery.cookie帮助类 (转载)

    /** * Cookie plugin * * Copyright (c) [url=http://sufei.cnblogs.com/]http://sufei.cnblogs.com[/url] ...

  6. jmeter,监控插件

    1.下载JMeterPlugins.jar 2.下载后放在\apache-jmeter-3.0\lib\ext下 3.重启jmeter,监听器中即可看到jp@gc-开头的监听器

  7. mysql的账户失效,之前的密码无法登录

     引用自:http://blog.sina.com.cn/s/blog_682c287b0100ofz8.html 此为linux服务器下的做法 方法一: 1.关闭mysql    # service ...

  8. 通过C#去调用C++编写的DLL

    这个问题缠了我2个小时才弄出来,其实很简单.当对方提供一个dll给你使用时,你需要去了解这个dll 是由什么语言写的,怎么编译的,看它的编译类型.这样即使在没有头绪时,你可以先尝使用一些比较热门的编译 ...

  9. (转) c# ExecuteNonQuery() 返回值 -1

    这是之前我遇到问题,在网上找解决方法时找到的,当时复制到txt文档了,今天整理笔记又看到了,贴出来,便于以后查阅.原文的作者没记住~~ 查询某个表中是否有数据的时候,如果用ExecuteNonQuer ...

  10. libctemplate——C语言模块引擎简介及使用

    前言 由先声明此libctemplate不是Google那个ctemplate.这个库是用C语言实现的,只有一个实现文件和一个头文件.Gooogl的ctemplate是C++实现的,和线程还扯上了关系 ...