码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode226 InvertBinaryTree Java题解

时间:2015-07-06 17:57:58      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:binarytree   java   leetcode   invertbinarytree   

题目:

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode226 InvertBinaryTree Java题解

标签:binarytree   java   leetcode   invertbinarytree   

原文地址:http://blog.csdn.net/u012249528/article/details/46776281

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!