0226-Easy-翻转二叉树
最后更新于
最后更新于
4
/ \
2 7
/ \ / \
1 3 6 9 4
/ \
7 2
/ \ / \
9 6 3 1/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
// 自定向下的思考方式
// 递归
// invert(root) = invert(root->left) + invert(root->right)
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
// https://www.jianshu.com/p/b2d2edb4ba5b
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}
}