lintcode :Invert Binary Tree 翻转二叉树
题目:
翻转一棵二叉树
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 翻转二叉树的更多相关文章
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- [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 ...
- 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)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- 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 ...
- 第27题:Leetcode226: Invert Binary Tree反转二叉树
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 思路 如果根节点存在,就交换两个子树的根节点,用递归 ...
- LeetCode Invert Binary Tree 反转二叉树
思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...
- 【easy】226. Invert Binary Tree 反转二叉树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- [LintCode] Identical Binary Tree 相同二叉树
Check if two binary trees are identical. Identical means the two binary trees have the same structur ...
随机推荐
- ThreadLocal模式探索
一.首先,ThreadLocal模式使共享数据能多个线程被访问,每个线程访问的只是这个数据的副本,线程之间互不影响. 例子1: package Thread2; public class Counte ...
- 单例模式C#
首先来明确一个问题,那就是在某些情况下,有些对象,我们只需要一个就可以了, 比如,一台计算机上可以连好几个打印机,但是这个计算机上的打印程序只能有一个, 这里就可以通过单例模式来避免两个打印作业同时输 ...
- Oracle SQL 劈开字符串
一.数据样例 二.劈开单行 , rownum) h2 FROM (select id_,name_ from test_reg_count t ) CONNECT ; --或者 , rownum) h ...
- android之TextView
TextView 常用属性说明: lines:设置可以显示的文本行数,且不管文本是否足够占用这些行的空间,该组件都会占用这些行的空间高度. maxLines:设置最大显示的行数,随文本改变,占用的行数 ...
- 问题 K: 【USACO2012Feb】植草 {Bronze题2}
按着矩形周长的思路,到当前边的时候,前一层的覆盖数乘以高度加入 ans 就行,然而真正的算法可能并不是这个..只能想到这个了 ; type node=record l,r,mid,sum,delta: ...
- cookie、session的联系和区别,多台web服务器如何共享session?
cookie在客户端保存状态,session在服务器端保存状态.但是由于在服务器端保存状态的时候,在客户端也需要一个标识,所以session也可能要借助cookie来实现保存标识位的作用.cookie ...
- C/C++中的可变参函数
可变参函数最好的实例:printf();参数可变 包含的头文件: C语言中:#include<stdarg.h> C++中的可变参的头文件:#include<cstdarg>, ...
- startDiscovery() and startLeScan().
You have to start a scan for Classic Bluetooth devices with startDiscovery() and a scan for Bluetoot ...
- bnuoj 4209 Triangle(计算几何)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=4209 题意:如题 题解:公式直接计算,或者角平分线求交点 [code1]: #include < ...
- 深入浅出C++引用(Reference)类型
要点1:为反复使用的.冗长的变量名称定义一个简短的.易用的别名,从而简化了代码.通常,冗长的变量名称源于多层嵌套对象,例如类中定义嵌套类,类中定义其它类对象. //------ 未使用引用的程序片段, ...