[leetcode]: 226. Invert Binary Tree
2017-06-01 21:56
288 查看
1.题目
Invert a binary tree.4 / \ 2 7 / \ / \ 1 3 6 9
to
4 / \ 7 2 / \ / \ 9 6 3 1
将一棵二叉树翻转。
左右子树,左右节点互换,相当于镜像对称。
2.分析
递归or迭代3.代码
递归class Solution { public: TreeNode* invertTree(TreeNode* root) { if (root == NULL) return root; TreeNode* tempL= invertTree(root->right); TreeNode* tempR = invertTree(root->left); root->left = tempL; root->right = tempR; return root; } };
广度优先遍历,queue
class Solution { public: TreeNode* invertTree(TreeNode* root) { if (root == NULL) return root; queue<TreeNode*> nodes; nodes.push(root); while (!nodes.empty()) { TreeNode* node = nodes.front(); nodes.pop(); if (node->left != NULL) nodes.push(node->left); if (node->right != NULL) nodes.push(node->right); TreeNode* temp = node->left; node->left = node->right; node->right = temp; } return root; } };
相关文章推荐
- Leetcode_226_Invert Binary Tree
- leetcode--226. Invert Binary Tree
- [LeetCode]226. Invert Binary Tree
- 【leetcode每日一题】226.Invert Binary Tree
- LeetCode:226. Invert Binary Tree
- leetcode:226. Invert Binary Tree
- 【LeetCode】226.Invert Binary Tree(Easy)解题报告
- 渣渣写LEETCODE——226. Invert Binary Tree
- [LeetCode 226] Invert Binary Tree
- LeetCode 226. Invert Binary Tree
- LeetCode 226 Invert Binary Tree
- leetcode 226. Invert Binary Tree
- LeetCode 226. Invert Binary Tree
- Leetcode 226[easy]---Invert Binary Tree(迭代入门题,tree-queue,stack 入门题,反复看)
- 【Leetcode】:226. Invert Binary Tree 问题 in JAVA
- leetcode 226: Invert Binary Tree
- LeetCode-226.Invert Binary Tree
- Leetcode 226 Invert Binary Tree
- Leetcode_226 Invert Binary Tree
- Leetcode226 Invert Binary Tree