您的位置:首页 > 其它

【Leetcode】Invert Binary Tree

2015-11-20 23:21 183 查看
题目链接:https://leetcode.com/problems/invert-binary-tree/

题目:

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

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


思路:

递归调整左右指针顺序就好了。

算法:

public TreeNode invertTree(TreeNode root) {
TreeNode tmp = null;
if (root == null) {
return root;
}
tmp = root.left;
root.left = root.right;
root.right = tmp;
invertTree(root.left);
invertTree(root.right);
return root;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: