【Leetcode】Invert Binary Tree

来源:互联网 发布:淘宝网电器城 编辑:程序博客网 时间:2024/06/06 20:31

题目链接: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;}



0 0
原创粉丝点击