Invert Binary Tree 解答
Quetion
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
Solution 1 -- Recursion
Easy to think.
/**
* 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 root;
TreeNode leftNode = null, rightNode = null;
if (root.left != null)
leftNode = invertTree(root.left);
if (root.right != null)
rightNode = invertTree(root.right);
root.left = rightNode;
root.right = leftNode;
return root;
}
}
Solution 2 -- Iteration
BFS
/**
* 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 root;
List<TreeNode> current = new ArrayList<TreeNode>();
List<TreeNode> next;
current.add(root);
while (current.size() > 0) {
next = new ArrayList<TreeNode>();
for (TreeNode tmpNode : current) {
// inverse tmpNode
TreeNode left = tmpNode.left;
TreeNode right = tmpNode.right;
tmpNode.right = left;
tmpNode.left = right;
if (right != null)
next.add(right);
if (left != null)
next.add(left);
}
current = next;
}
return root;
}
}
Invert Binary Tree 解答的更多相关文章
- 【07_226】Invert Binary Tree
Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy Invert a binary ...
- lc面试准备:Invert Binary Tree
1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...
- 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 ...
- 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 ...
- C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...
- [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
LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...
- 【Invert Binary Tree】cpp
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...
- <LeetCode OJ> 226. Invert Binary Tree
226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...
随机推荐
- wpf纯前台绑定
<Window x:Class="Example1.MainWindow" ... xmlns:local="clr-namespace:Example1" ...
- JQuery Ajax 获取数据
前台页面: 对一张进行查询,删除,添加 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"& ...
- ubuntu14.04 安装 StudioZend12
到官网下载:http://www.zend.com/en/products/studio/downloadsLinux-64位:http://downloads.zend.com/studio-ecl ...
- Test注解的两个属性:expected和timeout
JUnit4:Test文档中的解释: The Test annotation supports two optional parameters. The first, expected, declar ...
- PHP设计模式笔记二:面向对象 -- Rango韩老师 http://www.imooc.com/learn/236
SPL标准库的使用 SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 1.SPL提供了很多数据结构类,如SplStack.SqlQueue.SqlHeap.SplF ...
- DOS 选择跳转实现、dos + bcp 双击导入和导出数据
DOS 选择跳转实现.dos + bcp 双击导入和导出数据 option.bat @echo off :Start2 cls goto Start :Start title Frequently U ...
- iOS多线程及其感悟
感觉每天都是匆匆忙忙的,每天似乎都是时间不够用一样,可是等真的想要动手敲代码的时候才发现,原来还有好多好多的知识点不是太熟练,所以,人不可以一直感觉自我良好, 有时间就是那种自我感觉良好的心态毁了自己 ...
- JavaScript禁止用户多次提交方法
[当服务器超载时,会出现提交卡顿的现象,但是用户在操作时,会不停重复点击提交,会造成服务器压力更大.所以我们需要进行限制] [1]将提交按钮禁止 <html> <head> & ...
- TexturePacker文件的反向解析-TextureUnpacker
最近在使用cocos2d-x做开发,其中会用到TexturePacker工具打包纹理文件,但是有时候想从打包好的.plist和.png大图文件反向生成原始的小图文件,TexturePacker好像没有 ...
- git的使用与积累
之前对git可以说是一无所知,不过现在做工程要用到,于是就花点时间找了一些资料,本文也只是各种git学习资料的集合,权当是学习笔记吧 一:git的安装与配置 首先,git其实一般在linux环境下都是 ...