Easy-20
来源:互联网 发布:电话销售数据货源qq群 编辑:程序博客网 时间:2024/05/16 05:42
leetcode 226. Invert Binary Tree
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 1
AC;
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* invertTree(struct TreeNode* root) {
if(root==NULL){
return ;
}
struct TreeNode* node;
if(root->left!=NULL){
invertTree(root->left);
}
if(root->right!=NULL){
invertTree(root->right);
}
node=root->left;
root->left=root->right;
root->right=node;
return root;
}
if(root==NULL){
return ;
}
struct TreeNode* node;
if(root->left!=NULL){
invertTree(root->left);
}
if(root->right!=NULL){
invertTree(root->right);
}
node=root->left;
root->left=root->right;
root->right=node;
return root;
}
tips:开始准备想着遍历把值取出来,然后逆序,再遍历放回去。后来又发现只要遍历一次,遍历过程中交换左右子树即可。想出这个方法,对于菜鸟的我来说,很激动!~
0 0
- Easy-20
- easy
- Easy
- [Leetcode 20, Easy] Valid Parentheses
- <LeetCode><Easy> 20 Valid Parenthese
- 【Leetcode-Easy-20】 Valid Parentheses
- LeetCode-20-Valid Parentheses(堆栈)-Easy
- Easy-题目20:202. Happy Number
- 1019. General Palindromic Number (20)(-selfdone-easy)
- 蓝桥杯 ADV-20 算法提高 交换Easy
- Easy 5 Valid Parentheses(20)
- Easy 20 Same Tree(100)
- LeetCode OJ 20 Valid Parentheses [Easy]
- 算法提高 ADV-20 交换Easy
- LeetCode-Easy刷题(20) Symmetric Tree
- poj 2531(按位暴力枚举2^20很easy)
- NHK Easy News 翻译 12月20日-1
- NHK Easy News 翻译 12月20日-2
- vs2010快捷键
- URAL 2014
- h5学习笔记:UI库
- 寻找和固定的两个或三个数
- 剑指offer-面试题56-链表中环的入口结点
- Easy-20
- 使用ionic2开发项目
- Count the Sheep
- 安卓面试问题
- SDUT 1269 走迷宫
- Unity基础的射线使用和坐标转换
- 无线 WIFI 的13个信道频率范围
- 线程的信号pthread_kill()函数(线程四)
- Java中如何调用匿名内部类