您的位置:首页 > 编程语言 > Java开发

LeetCode226 InvertBinaryTree Java题解

2015-07-06 16:42 447 查看
题目:

Invert a binary tree.
4
/   \
2     7
/ \   / \
1   3 6   9

to
4
/   \
7     2
/ \   / \
9   6 3   1

解答:
遍历每一个节点 直接交换他们的左右节点

代码:

public static  TreeNode invertTree(TreeNode root) {

if(root!=null)
{
TreeNode temNode=root.left;
root.left=root.right;
root.right=temNode;
invertTree(root.left);
invertTree(root.right);
}

return root;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: