三刷
100.00 %
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root != null) {
TreeNode left = root.left;
root.left = invertTree(root.right);
root.right = invertTree(left);
}
return root;
}
}
二刷
完全一样
27.47 %
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) return null;
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}
}
一刷
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) return root;
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}
}
No comments:
Post a Comment