LeetCode 226. 翻转二叉树

我的LeetCode:https://leetcode-cn.com/u/ituring/

我的LeetCode刷题源码[GitHub]:https://github.com/izhoujie/Algorithmcii

LeetCode 226. 翻转二叉树

题目

翻转一棵二叉树。

示例:

输入:

     4
   /   
  2     7
 /    / 
1   3 6   9


输出:

     4
   /   
  7     2
 /    / 
9   6 3   1

备注:
这个问题是受到 Max Howell原问题 启发的 :

谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/invert-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路

思路1-递归互换左右子树

算法复杂度:

  • 时间复杂度: $ {color{Magenta}{Omicronleft(n ight)}} $
  • 空间复杂度: $ {color{Magenta}{Omicronleft(1 ight)}} $

算法源码示例

package leetcode;

/**
 * @author ZhouJie
 * @date 2020年1月10日 下午10:31:54 
 * @Description: 226. 翻转二叉树
 *
 */
public class LeetCode_0226 {

}

//Definition for a binary tree node.
class TreeNode_0226 {
	int val;
	TreeNode_0226 left;
	TreeNode_0226 right;

	TreeNode_0226(int x) {
		val = x;
	}
}

class Solution_0226 {
	public TreeNode_0226 invertTree(TreeNode_0226 root) {
		// 节点不为null时,左右节点交换,然后左右各自递归
		if (root != null) {
			TreeNode_0226 l = root.left;
			TreeNode_0226 r = root.right;
			root.left = invertTree(r);
			root.right = invertTree(l);
		}
		return root;
	}
}

原文地址:https://www.cnblogs.com/izhoujie/p/12832536.html