LeetCode226 InvertBinaryTree Java题解

来源:互联网 发布:mplayer移植arm linux 编辑:程序博客网 时间:2024/06/06 04:51

题目:

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;            }


0 0
原创粉丝点击