/**
* 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 {
int t;
public TreeNode invertTree(TreeNode root) {
//重要边界条件: null也要反转过去
if (root == null) {
return null;
}
//重要信息: 节点的交换
TreeNode node = root.right;
root.right = invertTree(root.left);
root.left = invertTree(node);
return root;
}
}
leetcode 226. 翻转二叉树
最新推荐文章于 2024-07-17 08:53:04 发布