题目:

翻转一棵二叉树

样例

  1         1
/ \ / \
2 3 => 3 2
/ \
4 4
挑战

递归固然可行,能否写个非递归的?

解题:

递归比较简单,非递归待补充

Java程序:

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
public void invertBinaryTree(TreeNode root) {
// write your code here
invertTree(root); }
public TreeNode invertTree(TreeNode root){
if( root ==null){
return root;
}
TreeNode rleft= root.left;
root.left = invertTree(root.right);
root.right = invertTree(rleft);
return root;
}
}

总耗时: 994 ms

Python程序:

"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
# @param root: a TreeNode, the root of the binary tree
# @return: nothing
def invertBinaryTree(self, root):
# write your code here
self.invertTree(root) def invertTree(self,root):
if root == None:
return root
rlft = root.left
root.left = self.invertTree(root.right)
root.right = self.invertTree(rlft)
return root

总耗时: 118 ms

参考剑指OfferP127改进了下程序,更容易理解了

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
public void invertBinaryTree(TreeNode root) {
// write your code here
invertTree(root);
}
public void invertTree(TreeNode root){
if( root ==null){
return;
}
if(root.left == null && root.right ==null){
return;
}
TreeNode tmp= root.left;
root.left = root.right;
root.right = tmp;
if(root.left!=null){
invertTree(root.left);
}
if(root.right!=null){
invertTree(root.right);
}
}
}

Java Code

 
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
# @param root: a TreeNode, the root of the binary tree
# @return: nothing
def invertBinaryTree(self, root):
# write your code here
if root == None:
return
if root.left==None and root.right ==None:
return
tmp = root.left
root.left = root.right
root.right = tmp
if root.left!=None:
self.invertBinaryTree(root.left)
if root.right!=None:
self.invertBinaryTree(root.right)

Python Code

lintcode :Invert Binary Tree 翻转二叉树的更多相关文章

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

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

  2. [LeetCode] 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 ...

  3. 226. Invert Binary Tree 翻转二叉树

    [抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...

  4. 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...

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

  6. 第27题:Leetcode226: Invert Binary Tree反转二叉树

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1  思路 如果根节点存在,就交换两个子树的根节点,用递归 ...

  7. LeetCode Invert Binary Tree 反转二叉树

    思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...

  8. 【easy】226. Invert Binary Tree 反转二叉树

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. [LintCode] Identical Binary Tree 相同二叉树

    Check if two binary trees are identical. Identical means the two binary trees have the same structur ...

随机推荐

  1. [精校版]The Swift Programming Language

    通常来说,编程语言教程中的第一个程序应该在屏幕上打印“Hello, world”.在 Swift 中,可以用一行代码实现:  println("hello, world")   如 ...

  2. JAVA多线程学习2--线程同步

    一.线程同步介绍 同步:就是协同步调,按照预定的先后顺序执行.比如:你说完我再说. 线程同步:访问同一个共享资源的时候多个线程能够保证数据的安全性.一致性. 二.JAVA中实现线程同步的方法 实现进程 ...

  3. linux 终端显示 -bash-4.1

    解决方法: cp /etc/skel/.bashrc /root/ cp /etc/skel/.bash_profile  /root/ 重新登陆就OK了

  4. 关于无限分类的树状输出(id,name,pid)类型的

    首先创建无限分类的数据表,我这里采用的是id.name.pid这种类型(当然还有很多种无限分类的方式了,比如:id.name.pid.path.left.right左右节点的形式) CREATE TA ...

  5. 通知栏发送消息Notification(可以使用自定义的布局)

    一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见.代码在此时机发送一个Notifica ...

  6. ASP.NET中的常用快捷键

    想查找ASP.NET中的属性快捷键,忘记了,搜了一下,找到了ASP.NET中的常用快捷键. 大神文章:http://www.cnblogs.com/xiacao/archive/2012/06/12/ ...

  7. Mininet安装及使用

    最简单的方法是开始 下载一个预包装Mininet / Ubuntu VM . 这个虚拟机包括Mininet本身,所有预装OpenFlow二进制文件和工具,调整内核配置,以支持更大的Mininet网络. ...

  8. 2016 系统设计第一期 (档案一)jQuery ajax serialize()方法form提交数据

    jQuery ajax serialize()方法form提交数据,有个很奇怪的问题,好像不能取到隐藏控件的值. //点击提交按钮保存数据 $('#btn_submitUser').click(fun ...

  9. java 进制转化

    public static void toBinary(int num){ trans(num,1,1); } public static void toHex(int num){ trans(num ...

  10. android studio 不能在线更新android SDK Manager问题解决办法

    Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml, reason: Connecti ...