(java)leetcode Invert Binary Tree
2015-10-30 18:24
417 查看
Invert a binary tree.
to
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 fuck off.
思路:普通的递归题目,就是把左右节点对称的进行交换
代码如下(已通过leetcode)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null) return null;
TreeNode temp=root.left;
root.left=invertTree(root.right);
root.right=invertTree(temp);
return root;
}
}
4 / \ 2 7 / \ / \ 1 3 6 9
to
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 fuck off.
思路:普通的递归题目,就是把左右节点对称的进行交换
代码如下(已通过leetcode)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null) return null;
TreeNode temp=root.left;
root.left=invertTree(root.right);
root.right=invertTree(temp);
return root;
}
}
相关文章推荐
- myeclipse2014闪退的解决方法
- struts2使用插件建立图表
- java内部类详解
- no GurobiJni55 in java.library.path
- java程序获取项目的路径
- java1.8--Optional类
- java1.8--Optional类
- Java内存泄露介绍
- Java开发中获得非Web项目的当前项目路径
- Eclipse汉化过程
- java final关键字
- jsp java
- Struts2防止form表单重复提交
- sigar进行JAVA后台系统资源监控错误
- java equals方法与==比较
- eclipse导出jar包
- 开源 java CMS - FreeCMS2.4字典管理
- 给定n个矩阵{A1, A2, …,An},其中,Ai与Ai+1是可乘的,计算这n个矩阵的连乘积。从中找出一种乘次数最少的计算次序(矩阵连乘最优顺序Java语言实现
- java中的HashSet和TreeSet的区别,以及HashSet导致的内存泄漏详解
- 【Struts】strust.xml中<result type="">所有类型详解