3.30
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
public void invertBinaryTree(TreeNode root) {
if(root == null){
return;
}
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
invertBinaryTree(root.left);
invertBinaryTree(root.right);
}
}
本文介绍了一种翻转二叉树的算法实现,通过递归方式交换二叉树节点的左右子节点,达到整体翻转的效果。文章提供了一个具体的Java代码示例。
393

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



