反转二叉树
作者:
夜未殇 | 来源:发表于
2017-04-11 17:47 被阅读0次/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {//一定要检查指针为空
return null;
}
root.left = invertTree(root.left);
root.right = invertTree(root.right);
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
return root;
}
}
本文标题:反转二叉树
本文链接:https://www.haomeiwen.com/subject/iryuattx.html
网友评论