leet-code/src/main/java/leetcode/editor/cn/InvertBinaryTree.java
2021-07-20 16:34:21 +08:00

68 lines
1.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//翻转一棵二叉树。
//
// 示例:
//
// 输入:
//
// 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)
}