[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 Solution {
public:
    /**
     * @param root: a TreeNode, the root of the binary tree
     * @return: nothing
     */
    void invertBinaryTree(TreeNode *root) {
        if (!root) return;
        TreeNode *tmp = root->left;
        root->left = root->right;
        root->right = tmp;
        invertBinaryTree(root->left);
        invertBinaryTree(root->right);
    }
};

解法二:

// Non-Recursion
class Solution {
public:
    /**
     * @param root: a TreeNode, the root of the binary tree
     * @return: nothing
     */
    void invertBinaryTree(TreeNode *root) {
        if (!root) return;
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {
            TreeNode* node = q.front(); q.pop();
            TreeNode *tmp = node->left;
            node->left = node->right;
            node->right = tmp;
            if (node->left) q.push(node->left);
            if (node->right) q.push(node->right);
        }
    }
};
时间: 2024-10-20 16:09:56

[LintCode] Invert Binary Tree 翻转二叉树的相关文章

[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

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 Invert Binary Tree 反转二叉树

思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solutio

[LintCode] Identical Binary Tree 相同二叉树

Check if two binary trees are identical. Identical means the two binary trees have the same structure and every identical position has the same value. Have you met this question in a real interview? Example 1 1 / \ / 2 2 and 2 2 / / 4 4 are identical

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

Python解Leetcode: 226. Invert Binary Tree

leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): d

LeetCode226:Invert Binary Tree 二叉树反转

题目 226. Invert Binary Tree QuestionEditorial Solution My Submissions Total Accepted: 109341 Total Submissions: 230799 Difficulty: Easy Invert a binary tree.      4    /     2     7  / \   / 1   3 6   9 to      4    /     7     2  / \   / 9   6 3   1

【Invert Binary Tree】cpp

题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solution 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:

leetcode_226题——Invert Binary Tree(队列,广度优先搜索)

Invert Binary Tree Total Accepted: 22352 Total Submissions: 62065My Submissions Question Solution 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%