题目链接
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if(root!=null)
{
TreeNode A=root.left;
root.left=root.right;
root.right=A;
invertTree(root.left);
invertTree(root.right);
}
return root;
}
}
本文提供了一种翻转二叉树的算法实现,通过递归方式交换每个节点的左右子树来完成整个二叉树的翻转。该算法简单高效,易于理解和实现。
625

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



