[LintCode] 翻转二叉树

递归实现:

 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14 public:
15     /**
16      * @param root: a TreeNode, the root of the binary tree
17      * @return: nothing
18      */
19     void invertBinaryTree(TreeNode *root) {
20         // write your code here
21         if (!root) return;
22         swap(root -> left, root -> right);
23         invertBinaryTree(root -> left);
24         invertBinaryTree(root -> right);
25     }
26 };

迭代实现:

 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14 public:
15     /**
16      * @param root: a TreeNode, the root of the binary tree
17      * @return: nothing
18      */
19     void invertBinaryTree(TreeNode *root) {
20         // write your code here
21         if (!root) return;
22         queue<TreeNode*> level;
23         level.push(root);
24         while (!level.empty()) {
25             TreeNode* node = level.front();
26             level.pop();
27             swap(node -> left, node -> right);
28             if (node -> left) level.push(node -> left);
29             if (node -> right) level.push(node -> right);
30         }
31     }
32 };
时间: 2024-07-29 06:25:07

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

[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

[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; } * } */

226. 翻转二叉树

翻转一棵二叉树. 示例: 输入: 4 / 2 7 / \ / 1 3 6 9 输出: 4 / 7 2 / \ / 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题 启发的 : 谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeN

226. 翻转二叉树 | Invert Binary Tree

Invert a binary tree. Example: Input: 4 / 2 7 / \ / 1 3 6 9 Output: 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

LeetCode226 翻转二叉树

翻转一棵二叉树. 示例: 输入: 4 / 2 7 / \ / 1 3 6 9 输出: 4 / 7 2 / \ / 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题 启发的 : 谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left;

leecode刷题(24)-- 翻转二叉树

leecode刷题(24)-- 翻转二叉树 翻转二叉树 翻转一棵二叉树. 示例: 输入: 4 / 2 7 / \ / 1 3 6 9 输出: 4 / 7 2 / \ / 9 6 3 1 备注: 这个问题是受到 Max Howell的 原问题 启发的 : 谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了. 思路 二叉树问题,我们首先要想到的使用递归的方式来解决,有了这个思路,处理这道问题就很简单了:先互换根节点的左右节点,然

算法 翻转二叉树 dfs

翻转二叉树 翻转一棵二叉树.左右子树交换. Example 样例 1: 输入: {1,3,#} 输出: {1,#,3} 解释: 1 1 / => 3 3 样例 2: 输入: {1,2,3,#,#,4} 输出: {1,3,2,#,4} 解释: 1 1 / \ / 2 3 => 3 2 / 4 4 Challenge 递归固然可行,能否写个非递归的? 代码: """ Definition of TreeNode: class TreeNode: def __init_

翻转二叉树(递归与非递归)

翻转一棵二叉树 样例 1 1 / \ / 2 3 => 3 2 / 4 4 递归版本 先翻转左子树,后翻转右子树,然后对整个树进行翻转 void swapTree(TreeNode *&root){ TreeNode *tmp = root->left; root->left = root->right; root->right = tmp; } void invertBinaryTree(TreeNode *root) { // write your code he