LeetCode 226. 翻转二叉树

深藏阁楼爱情的钟 2023-02-15 05:24 62阅读 0赞

在这里插入图片描述

  1. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
  2. class Solution {
  3. public TreeNode invertTree(TreeNode root) {
  4. if(root == null || (root != null && root.left == null && root.right == null)) return root;
  5. TreeNode ileft = invertTree(root.left);
  6. TreeNode iright = invertTree(root.right);
  7. root.left = iright;
  8. root.right = ileft;
  9. return root;
  10. }
  11. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,62人围观)

还没有评论,来说两句吧...

相关阅读

    相关 LeetCode226. 翻转

    今日份打卡力扣,这次用的是Java语言,之前都是用C/C++写的算法题,因为目前是从事Java开发方向,一会用C/C++,一会用Java,容易混淆,而且自己C++学的也不好,所