226:翻转二叉树
This commit is contained in:
parent
3445011c87
commit
18906b8100
68
src/main/java/leetcode/editor/cn/InvertBinaryTree.java
Normal file
68
src/main/java/leetcode/editor/cn/InvertBinaryTree.java
Normal file
@ -0,0 +1,68 @@
|
||||
//翻转一棵二叉树。
|
||||
//
|
||||
// 示例:
|
||||
//
|
||||
// 输入:
|
||||
//
|
||||
// 4
|
||||
// / \
|
||||
// 2 7
|
||||
// / \ / \
|
||||
//1 3 6 9
|
||||
//
|
||||
// 输出:
|
||||
//
|
||||
// 4
|
||||
// / \
|
||||
// 7 2
|
||||
// / \ / \
|
||||
//9 6 3 1
|
||||
//
|
||||
// 备注:
|
||||
//这个问题是受到 Max Howell 的 原问题 启发的 :
|
||||
//
|
||||
// 谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。
|
||||
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树
|
||||
// 👍 919 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
//226:翻转二叉树
|
||||
public class InvertBinaryTree{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new InvertBinaryTree().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public TreeNode invertTree(TreeNode root) {
|
||||
if (root == null) {
|
||||
return null;
|
||||
}
|
||||
TreeNode left = invertTree(root.left);
|
||||
root.left = invertTree(root.right);
|
||||
root.right = left;
|
||||
return root;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
25
src/main/java/leetcode/editor/cn/InvertBinaryTree.md
Normal file
25
src/main/java/leetcode/editor/cn/InvertBinaryTree.md
Normal file
@ -0,0 +1,25 @@
|
||||
<p>翻转一棵二叉树。</p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<p>输入:</p>
|
||||
|
||||
<pre> 4
|
||||
/ \
|
||||
2 7
|
||||
/ \ / \
|
||||
1 3 6 9</pre>
|
||||
|
||||
<p>输出:</p>
|
||||
|
||||
<pre> 4
|
||||
/ \
|
||||
7 2
|
||||
/ \ / \
|
||||
9 6 3 1</pre>
|
||||
|
||||
<p><strong>备注:</strong><br>
|
||||
这个问题是受到 <a href="https://twitter.com/mxcl" target="_blank">Max Howell </a>的 <a href="https://twitter.com/mxcl/status/608682016205344768" target="_blank">原问题</a> 启发的 :</p>
|
||||
|
||||
<blockquote>谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。</blockquote>
|
||||
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>广度优先搜索</li><li>二叉树</li></div></div>\n<div><li>👍 919</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user