力扣hot100 翻转二叉树 递归

2024-01-07 20:22:33

👨?🏫 题目地址
在这里插入图片描述

😋 AC code

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root)
	{
        if(root == null)
            return root;
		TreeNode t = root.left;
		root.left = root.right;
		root.right = t;
		if (root.left != null)
			invertTree(root.left);
		if (root.right != null)
			invertTree(root.right);
		return root;
	}
}

文章来源:https://blog.csdn.net/lt6666678/article/details/135371349
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。