[LintCode] Invert Binary Tree 翻转二叉树
二叉树 Tree Binary 翻转 lintcode
2023-09-11 14:21:39 时间
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); } } };
相关文章
- 1110 Complete Binary Tree (25 分)【难度: 一般 / 知识点: 判断完全二叉树】
- [LeetCode] 971. Flip Binary Tree To Match Preorder Traversal 翻转二叉树以匹配先序遍历
- [LeetCode] 1028. Recover a Tree From Preorder Traversal 从先序遍历还原二叉树
- [LeetCode] 662. Maximum Width of Binary Tree 二叉树的最大宽度
- [LeetCode] Print Binary Tree 打印二叉树
- [LeetCode] 545. Boundary of Binary Tree 二叉树的边界
- [LintCode] Identical Binary Tree 相同二叉树
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
- [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表
- [LeetCode] Balanced Binary Tree 平衡二叉树
- leetcode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 (中等)
- leetcode 144. Binary Tree Preorder Traversal 二叉树展开为链表(中等)
- leetcode 101. Symmetric Tree 对称二叉树(简单)