Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2cfc16f

Browse files
committed
solve 226.翻转二叉树
1 parent deb09e7 commit 2cfc16f

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

‎zh/226.翻转二叉树.java‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* @lc app=leetcode.cn id=226 lang=java
3+
*
4+
* [226] 翻转二叉树
5+
*
6+
* https://leetcode-cn.com/problems/invert-binary-tree/description/
7+
*
8+
* algorithms
9+
* Easy (75.17%)
10+
* Likes: 462
11+
* Dislikes: 0
12+
* Total Accepted: 83.9K
13+
* Total Submissions: 111.4K
14+
* Testcase Example: '[4,2,7,1,3,6,9]'
15+
*
16+
* 翻转一棵二叉树。
17+
*
18+
* 示例:
19+
*
20+
* 输入:
21+
*
22+
* ⁠ 4
23+
* ⁠ / \
24+
* ⁠ 2 7
25+
* ⁠/ \ / \
26+
* 1 3 6 9
27+
*
28+
* 输出:
29+
*
30+
* ⁠ 4
31+
* ⁠ / \
32+
* ⁠ 7 2
33+
* ⁠/ \ / \
34+
* 9 6 3 1
35+
*
36+
* 备注:
37+
* 这个问题是受到 Max Howell 的 原问题 启发的 :
38+
*
39+
* 谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。
40+
*
41+
*/
42+
43+
// @lc code=start
44+
/**
45+
* Definition for a binary tree node.
46+
* public class TreeNode {
47+
* int val;
48+
* TreeNode left;
49+
* TreeNode right;
50+
* TreeNode(int x) { val = x; }
51+
* }
52+
*/
53+
class Solution {
54+
public TreeNode invertTree(TreeNode root) {
55+
if (root == null) {
56+
return root;
57+
}
58+
TreeNode left = invertTree(root.right);
59+
TreeNode right = invertTree(root.left);
60+
root.left = left;
61+
root.right = right;
62+
return root;
63+
}
64+
}
65+
// @lc code=end
66+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /