Easy
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
0ms:
public TreeNode invertTree(TreeNode root) {
if (root != null) {
TreeNode tmp = root.left;
root.left = invertTree(root.right);
root.right = invertTree(tmp);
}
return root;
}
本文介绍了一种翻转二叉树的算法实现,通过递归方式交换二叉树节点的左右子节点,最终实现整棵树的翻转。提供了一个0毫秒运行的Java代码示例。
315

被折叠的 条评论
为什么被折叠?



