package 递归.q226_翻转二叉树;
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
package 递归.q226_翻转二叉树;
/**
* 递归 o(n)
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
if (root.left != null) {
invertTree(root.left);
}
if (root.right != null) {
invertTree(root.right);
}
return root;
}
}
q226_翻转二叉树
作品《LeetCode题目分类与面试问题整理 - q226_翻转二叉树》由 不喝星巴克 发布于 匠果,转载请注明出处及链接地址:
http://www.jiangguo.net/c/9r6/9e4.html