题目原址
https://leetcode.com/problems/invert-binary-tree/description/
题目描述
Invert a binary tree.
Example:
Input:
4
/ \
2 7
/ \ / \
1 3 6 9
Output:
4
/ \
7 2
/ \ / \
9 6 3 1
给定一个二叉树,交换其左右子树的节点值
AC代码
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null)
return null;
TreeNode tn = root.left;
root.left = root.right;
root.right = tn;
invertTree(root.left);
invertTree(root.right);
return root;
}
}

本文介绍了一种翻转二叉树的算法实现,通过递归方式交换二叉树每个节点的左右子树,最终达到整个二叉树的翻转效果。提供了完整的AC代码示例。
302

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



