[leetcode] 226.Invert Binary Tree
来源:互联网 发布:淘宝电商培训有哪些 编辑:程序博客网 时间:2024/05/01 17:42
题目:
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; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* invertTree(TreeNode* root) { if(root == NULL)return NULL; TreeNode* left = root->left; root->left = invertTree(root->right); root->right = invertTree(left); return root; }};
0 0
- 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
- 226. Invert Binary Tree LeetCode
- 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
- #leetcode#226. Invert Binary Tree
- LeetCode *** 226. Invert Binary Tree
- leetcode: 226. Invert Binary Tree
- js var的作用
- 禁用树莓派的xWindows休眠
- 如何在CentOS6.5下编译64位的Hadoop2.x?
- (LeetCode)旋转数组
- 【mysql】mysql中主外键关系(详细精讲)
- [leetcode] 226.Invert Binary Tree
- Chrome插件开发教程
- MTK65XX平台充电调试总结
- 安装SQL Server 2000出现“以前进行的程序安装创建了挂起的文件操作”的解决办法
- C语言循环的测试
- SIFT特征提取分析 sift算子
- JS: Cannot read property 'innerHTML' of undefined错误
- 布局优化
- 杭电 1061