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 ...
随机推荐
- Longest Common Prefix 解答
Question Write a function to find the longest common prefix string amongst an array of strings. Solu ...
- tabbar 嵌套 navigation
-------------- 源代码:点击打开链接 ------------------------ AppDelegate.m - (BOOL)application:(UIApplication ...
- [Immutable.js] Exploring Sequences and Range() in Immutable.js
Understanding Immutable.js's Map() and List() structures will likely take you as far as you want to ...
- Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6642463 在前面几篇文章中,我们详细介绍了A ...
- 大到可以小说的Y组合子(零)
问:啊!我想要一个匿名的递归… 答:Y(音同Why)… … … 问:作为一位命令式语言的使用者,为什么会突然折腾起Y组合子呢? 答:的确,这事儿要从很久以前的几次搁浅开始说起…上学的时候,从来没有接触 ...
- 引用System.Runtime.Serialization.Json
vs2012下,重新添加一次System.Runtime.Serialization的引用
- 前端--关于客户端javascript
浏览器中的Javascript 客户端javascript就是运行在浏览器中的javascript,现代的浏览器已经有了很好的发展,虽然它是一个应用程序,但完全可以把它看作是一个简易的操作系统,因为像 ...
- java序列化ClassNotFoundException
简单的想从保存的对象中重新解析出对象,用了逆序列化,可是报错: java.lang.ClassNotFoundException: xxxxxxxxxxxx at java.net.URLClassL ...
- (原+转)Ubuntu下安装understand及在启动器中增加快捷方式
参考网址: http://www.xuebuyuan.com/1353431.html http://www.2cto.com/os/201309/242543.html http://my.osch ...
- How to Use Javascript to Control Quicktime
参看下面链接: How to Use Javascript to Control Quicktime;