LeetCode Top 100 Liked Questions 226. Maximal Square (Java版; Medium)
题目描述
Invert a binary tree.
Example:
Input:
4
/ \
2 7
/ \ / \
1 3 6 9
Output:
4
/ \
7 2
/ \ / \
9 6 3 1
第一次做; 核心: 写递归函数之前是这么思考的: root的left指向反转好的右子树, root的right指向反转好的左子树; 因为需要子树返回子树的根节点, 所以当前递归函数也得返回当前树的根节点
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null)
return null;
TreeNode leftRes = invertTree(root.left);
TreeNode rightRes = invertTree(root.right);
root.left = rightRes;
root.right = leftRes;
return root;
}
}