原题
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
代码分析
反转二叉树
代码实现
public TreeNode InvertTree(TreeNode root)
{
if (root == null)
return null;
var tmp = root.left;
root.left = root.right;
root.right = tmp;
InvertTree(root.left);
InvertTree(root.right);
return root;
}

本文介绍了一种简单有效的反转二叉树算法。通过递归方式交换二叉树的左右子节点,达到整个树结构翻转的效果。文章提供了完整的代码实现,并通过一个具体的例子展示了反转前后的变化。
634

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



