C语言递归之翻转二叉树
题目描述
翻转一棵二叉树。
示例
输入:
/ \
/ \ / \
输出:
/ \
/ \ / \
题目要求
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ struct TreeNode* invertTree(struct TreeNode* root){ }
题解
/**
* 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 root;
struct TreeNode *r=(struct TreeNode*)malloc(sizeof(struct TreeNode));
r->val=root->val;
if(root->left!=NULL)r->right=invertTree(root->left);
else r->right=NULL;
if(root->right!=NULL)r->left=invertTree(root->right);
else r->left=NULL;
return r;
}
题解
递归
递归需要明确终止条件、返回值、递归内容,二叉树问题还需注意根节点为空的特殊情况。
题目来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/invert-binary-tree/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
C语言递归之翻转二叉树的更多相关文章
- C语言递归之对称二叉树
题目描述 给定一个二叉树,检查它是否是镜像对称的. 示例 二叉树 [1,2,2,3,4,4,3] 是对称的. / \ / \ / \ [1,2,2,null,3,null,3] 则不是镜像对称的. / ...
- [LeetCode] Invert Binary Tree 翻转二叉树
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...
- lintcode :Invert Binary Tree 翻转二叉树
题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...
- leetcode 翻转二叉树
翻转二叉树的步骤: 1.翻转根节点的左子树(递归调用当前函数) 2.翻转根节点的右子树(递归调用当前函数) 3.交换根节点的左子节点与右子节点 class Solution{ public: void ...
- C 语言实例 - 字符串翻转
C 语言实例 - 字符串翻转 C 语言实例 C 语言实例 使用递归来翻转字符串. 实例 - 字符串翻转 #include <stdio.h> void reverseSentence(); ...
- 算法 翻转二叉树 dfs
翻转二叉树 翻转一棵二叉树.左右子树交换. Example 样例 1: 输入: {1,3,#} 输出: {1,#,3} 解释: 1 1 / => \ 3 3 样例 2: 输入: {1,2,3,# ...
- leecode刷题(24)-- 翻转二叉树
leecode刷题(24)-- 翻转二叉树 翻转二叉树 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 ...
- Java实现 LeetCode 226 翻转二叉树
226. 翻转二叉树 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max ...
- LeetCode226 翻转二叉树
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题 ...
随机推荐
- filebeat configure
docker run -d --rm -v ./filebeat.yml:/usr/share/filebeat/filebeat.yml -v /var/log:/var/log docker.e ...
- vue 多层组件相互嵌套的时候 数据源更新 dom没更新 彻底清除组件缓存
当项目中存在多层组件相互嵌套 组件存在严重缓存时 this.$nextTick(() => { ..... }); 不管用 this.$forceUpdate(); 不管用 只能通过深拷贝浅拷 ...
- Educational Codeforces Round 50 (Rated for Div. 2) F - Relatively Prime Powers(数学+容斥)
题目链接:http://codeforces.com/contest/1036/problem/F 题意: 题解:求在[2,n]中,x != a ^ b(b >= 2 即为gcd)的个数,那么实 ...
- mysql5.7备份
一.备份准备&备份测试 1.备份目录准备 #mysql专用目录 mkdir /mysql #mysql备份目录 mkdir /mysql/backup #mysql备份脚本 mkdir /my ...
- Codeforces Round #452 (Div. 2) 899E E. Segments Removal
题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...
- 【VS调试】VS调试的时候使用IP地址,局域网的设备可以访问并调试
使用IIS Express调试,只能通过 http://localhost:端口 进行访问 客户端的设备如何才能通过 http://ip地址:端口 访问后台程序进行调试呢? 第一步,打开项目属性, ...
- learning gcc __BEGIN_DECLS and __END_DECLS
__BEGIN_DECLS and __END_DECLS be use for mix C and C++
- 收藏一个ST表模板
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...
- Transformer的PyTorch实现--转载
转载自 https://blog.csdn.net/stupid_3/article/details/83184691
- javascript中的class类 以及class的继承
在上面的章节中我们看到了JavaScript的对象模型是基于原型实现的,特点是简单,缺点是理解起来比传统的类-实例模型要困难,最大的缺点是继承的实现需要编写大量代码,并且需要正确实现原型链. 有没有更 ...