力扣——翻转二叉树

翻转一棵二叉树。

示例:

输入:

     4
   /     2     7
 / \   / 1   3 6   9

输出:

     4
   /     7     2
 / \   / 9   6 3   1
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
       if (root == null) {
            return null;
        }
        //当左右树都为空的时候不翻转,左右其中一个非空就翻转
        if (!(root.right == null && root.left == null)) {
            TreeNode right = root.right;
            TreeNode left = root.left;
            root.right = left;
            root.left = right;
            //翻转下一次层级,判断非空
            if (root.right != null) {
                invertTree(root.right);
            }
            //翻转下一次层级,判断非空
            if (root.left != null) {
                invertTree(root.left);
            }
        }

        return root;
    }
}

原文地址:https://www.cnblogs.com/JAYPARK/p/10352001.html

时间: 2024-07-31 15:22:33

力扣——翻转二叉树的相关文章

[力扣]144_二叉树的前序遍历

/* 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. */ 方法一:常规递归方式,用C语言实现(根左右) 代码实现: /** * Note

力扣——合并二叉树

给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点. 示例 1: 输入: Tree 1 Tree 2 1 2 / \ / \ 3 2 1 3 / \ \ 5 4 7 输出: 合并后的树: 3 / 4 5 / \ \ 5 4 7 /** * Definition for a binary tree node.

力扣——单值二叉树

如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树. 只有给定的树是单值二叉树时,才返回 true:否则返回 false. 示例 1: 输入:[1,1,1,1,1,null,1] 输出:true 示例 2: 输入:[2,2,2,5,2] 输出:false 提示: 给定树的节点数范围是 [1, 100]. 每个节点的值都是整数,范围为 [0, 99] . /** * Definition for a binary tree node. * public class TreeNode {

力扣——翻转矩阵后的得分

有一个二维矩阵 A 其中每个元素的值为 0 或 1 . 移动是指选择任一行或列,并转换该行或列中的每一个值:将所有 0 都更改为 1,将所有 1 都更改为 0. 在做出任意次数的移动后,将该矩阵的每一行都按照二进制数来解释,矩阵的得分就是这些数字的总和. 返回尽可能高的分数. 示例: 输入:[[0,0,1,1],[1,0,1,0],[1,1,0,0]] 输出:39 解释: 转换为 [[1,1,1,1],[1,0,0,1],[1,1,1,1]] 0b1111 + 0b1001 + 0b1111 =

JS数据结构第六篇 --- 二叉树力扣练习题

1.第226题:翻转二叉树 递归+迭代两种实现方式: /** 反转二叉树 * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {TreeNode} * 第一种方式迭代 * 执行用时 :72 ms, 在所有 JavaScript 提

力扣——二叉树的层平均值

给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入:    3   / \  9  20    /  \   15   7输出: [3, 14.5, 11]解释:第0层的平均值是 3,  第1层是 14.5, 第2层是 11. 因此返回 [3, 14.5, 11]. 注意: 节点值的范围在32位有符号整数范围内. 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/average-of-levels-in-binary-

[LeetCode] Invert Binary Tree 翻转二叉树

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a w

算法之:翻转二叉树

事情大概是说,Max Howell 去 Google 面试,面试官说:虽然在 Google 有 90% 的工程师用你写的 Homebrew,但是你居然不能在白板上写出翻转二叉树的代码,所以滚蛋吧. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */

[LintCode] Invert Binary Tree 翻转二叉树

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Example Given 4 points: (1,2), (3,6), (0,0), (1,3). The maximum number is 3. LeeCode上的原题,可参见我之前的博客Invert Binary Tree 翻转二叉树. 解法一: // Recursion class So