【LeetCode】226. Invert Binary Tree 解题报告



转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51527554


Subject

出处:https://leetcode.com/problems/invert-binary-tree/

Invert a binary tree.

     4
   /     2     7
 / \   / 1   3 6   9

to

     4
   /     7     2
 / \   / 9   6 3   1

Explain

该题目相当简单,就是一个简单的二叉树左右对换结点。


Solution

solution 1

递归方式

从第一层向下的顺序对调当前节点的左右子数。

    /**
     * 从上往下<br />
     * 0ms <br />
     *
     * @param root
     * @return
     */
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }

        TreeNode tempNode = root.left;
        root.left = root.right;
        root.right = tempNode;

        invertTree(root.left);
        invertTree(root.right);

        return root;
    }

LeetCode 平台 Run Time 竟然是 0ms

猴赛雷~~


solution 2

既然从上往下的顺序可以,那么从底层向上操作也是可以的。

    /**
     * 从下向上对换
     *
     * @param root
     * @return
     */
    public TreeNode invertTree2(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode leftNode = root.left;
        TreeNode rightNode = root.right;
        if (root.left != null) {
            leftNode = invertTree2(root.left);
        }
        if (root.right != null) {
            rightNode = invertTree2(root.right);
        }
        root.left = rightNode;
        root.right = leftNode;

        return root;
    }

以上两种都是递归操作方式。

其实用非递归也是可以的。大家可以自己去尝试~



bingo~~

时间: 2024-11-08 00:20:04

【LeetCode】226. Invert Binary Tree 解题报告的相关文章

leetcode 226 Invert Binary Tree

题目连接 https://leetcode.com/problems/invert-binary-tree/ Invert Binary Tree Description Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * Tre

Java for LeetCode 226 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: 解题思路: 递归即可,JAVA实现如下: public TreeNode invertTree(TreeNode root) { if(root==null) return root; TreeNode temp=ro

Leetcode 226 Invert Binary Tree python

题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过python中可以通过:a, b = b, a交换两个变量的值 1 class Solution(object): 2 def invertTree(self, root): 3 if root == None: return root 4 root.left, root.right = self.inve

LeetCode 226. 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 wh

leetcode 226 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

LeetCode 226 Invert Binary Tree(转换二叉树)

翻译 将下图中上面的二叉树转换为以下的形式.详细为每一个左孩子节点和右孩子节点互换位置. 原文 如上图 分析 每次关于树的题目出错都在于边界条件上--所以这次细致多想了一遍: void swapNode(TreeNode* tree) { if (tree == NULL || (tree->left == NULL && tree->right == NULL)) {} else if (tree->left == NULL && tree->ri

leetcode 226. 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 wh

LeetCode (226):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 wh

[LeetCode] 226. Invert Binary Tree JAVA

题目: Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 题意及分析:给出一棵二叉树,对每个节点交换左右子树.对于一个需要交换的节点有三种情况:(1)有左子树,无右子树:(2)有右子树,无左子树:(3)左右子树都有:对着三种情况分别交换左右子树,然后继续对子树递归即可得到结果. 代码: /** * Definition for a binary tree node. * public class TreeNo