Invert Binary Tree
来源:互联网 发布:c语言100经典实例编程 编辑:程序博客网 时间:2024/06/05 05:42
题目:
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
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) { TreeNode *temp; if(root!=NULL) { if(root->left==NULL&&root->right==NULL) { return root; } else if(root->left!=NULL&&root->right==NULL) { root->right=invertTree(root->left); root->left=NULL; } else if(root->left==NULL&&root->right!=NULL) { root->left=invertTree(root->right); root->right=NULL; } else { temp=root->right; root->right=invertTree(root->left); root->left=invertTree(temp); //res->val=root->val; } } return root; }};
0 0
- [leetcode] Invert Binary Tree
- [LeetCode]Invert Binary Tree
- Invert Binary Tree
- leetcode:Invert Binary Tree
- Invert Binary Tree
- LeetCode Invert Binary Tree
- 226Invert Binary Tree
- LeetCode | Invert Binary Tree
- leetcode--Invert Binary Tree
- [leetcode] Invert Binary Tree
- [LeetCode] Invert Binary Tree
- LeetCode Invert Binary Tree
- Invert Binary Tree
- leetcode : Invert Binary Tree
- 226Invert Binary Tree
- Invert Binary Tree
- [LeetCode] Invert Binary Tree
- [Leetcode]Invert Binary Tree
- 价格录入input
- 入门经典--硬币问题dp递归加递推
- SpringAOP:简单的Aspect案例
- Android平台下实现一个进程管理器
- Mercer定理
- Invert Binary Tree
- intellij idea常用快捷键使用
- Windows下使用Redis(一)安装使用
- React Native学习01 - 在Mac平台上搭建iOS开发环境
- 磁盘临时表与内存临时表的差异
- Android-自定义view之圆(选择程度圆以及进度圆)详解
- State Threads——异步回调的线性实现
- Java文件拆分与合并
- HDU 2328 Corporate Identity(后缀数组-求多个串的最长共同子串)