Invert Binary Tree
Description
Invert a binary tree.Left and right subtrees exchange.
/**
* 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) {
// write your code here
if(root == null){
return ;
}
TreeNode temp = root.left ;
root.left = root.right ;
root.right = temp ;
invertBinaryTree(root.left) ;
invertBinaryTree(root.right) ;
}
}
这是一个关于计算机科学的问题,涉及二叉树的数据结构。解决方案提供了一个Java方法来翻转给定的二叉树,交换其左右子树。递归地对左子树和右子树进行相同操作,直到所有节点都被处理,完成二叉树的反转。
639

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



