首页 > 其他分享 >LeetCode刷题(9)【树】前序&深度&平衡(C语言)

LeetCode刷题(9)【树】前序&深度&平衡(C语言)

时间:2022-11-17 22:03:53浏览次数:44  
标签:right TreeNode struct int 前序 C语言 return root LeetCode


二叉树的前序遍历

144. 二叉树的前序遍历 - 力扣(LeetCode) (leetcode-cn.com)

本题中,对于C++或者Java等语言,返回的是它们的数据结构库里面的数据结构,而C语言没有,这也就是如果用C语言往后通吃数据结构会困难的原因。

注意本体的传参,操作的是不是一个数。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/


/**
* Note: The returned array must be malloced, assume caller calls free().
*/
//计算结点个数
int TreeSize(struct TreeNode* root)
{
return root == NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
}
//前序遍历
void _preorder(struct TreeNode* root,int* a,int *i)//为了保证一直对一个i进行操作所以要传地址
{
if(root == NULL)
{
return ;
}
a[*i] = root->val;
(*i)++;
_preorder(root->left,a,i);
_preorder(root->right,a,i);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int size = TreeSize(root);
//创建数组
int* a = (int*)malloc(size*sizeof(int));
int i = 0;
_preorder(root,a,&i);
*returnSize = size;
return a;
}

二叉树的最大深度

经典的分治问题

104. 二叉树的最大深度 - 力扣(LeetCode) (leetcode-cn.com)

一棵树的高度就是最长路径的结点个数。

  • 空 - 高度为0
  • 非空 左右子树深度大的内个+1

本质上用的后序遍历,先求左,后求右边,再求自己。

图示

LeetCode刷题(9)【树】前序&深度&平衡(C语言)_数据结构

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/


int maxDepth(struct TreeNode* root){
if(root == NULL)
return 0;
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);

return leftDepth > rightDepth?leftDepth+1:rightDepth+1;
}

平衡二叉树

Loading Question… - 力扣(LeetCode) (leetcode-cn.com)

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxDepth(struct TreeNode* root){
if(root == NULL)
return 0;
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);

return leftDepth > rightDepth?leftDepth+1:rightDepth+1;
}

bool isBalanced(struct TreeNode* root){
//空树也满足条件
if(root == NULL)
{
return true;
}
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
//如果一开始就不满足就没必要往下进行了,满足就递归判断左右
return abs(leftDepth-rightDepth) < 2
&& isBalanced(root->left)
&& isBalanced(root->right);
}


标签:right,TreeNode,struct,int,前序,C语言,return,root,LeetCode
From: https://blog.51cto.com/u_15333750/5866194

相关文章

  • C语言文件操作
    相关视频——C语言精华——C语言文件操作,文件打开、关闭、读取、定位如何操作?为你逐一讲解文件操作标准库函数_哔哩哔哩(゜-゜)つロ干杯~-bilibili我的小站——半生瓜のbl......
  • C语言实现学生成绩管理系统
    相关视频——https://www.bilibili.com/video/BV13z4y117qC?p=8我的小站——半生瓜のblog​代码​实现#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>#include<conio.......
  • LeetCode刷题(1)【链表】【反转链表】(C语言)
    我的小站——半生瓜のblog(doraemon2.xyz)题目链接——206.反转链表-力扣(LeetCode)(leetcode-cn.com)反转链表思路一:反转指针。本质上就是调转指针的方向。首先我们......
  • 【链表】双向循环带头链表-增-删-查(C语言)
    我的小站——半生瓜のblog——同步更新单链表存在的缺陷:不能从后往前走,找不到他的前驱,指定位置删除增加尾删都要找前一个,时间复杂度都是O(n)针对上面的这些缺陷的解决......
  • 【链表】单链表-增-删-查(C语言)
    我的小站——半生瓜のblog单链表​​结构体定义​​​​打印​​​​创建结点​​​​尾插​​​​头插​​​​尾删​​​​头删​​​​查找​​​​在指定位置前插入某个......
  • 【线性表】之顺序表(C语言)
    【线性表】之顺序表​​线性表​​​​顺序表​​​​结构定义​​​​初始化​​​​销毁​​​​打印​​​​扩展空间​​​​尾插​​​​头插​​​​尾删​​​​头删......
  • LeetCode刷题(4)【移除元素&合并两个有序数组】(C语言)(含图解)
    移除元素典型双指针玩法。27.移除元素-力扣(LeetCode)(leetcode-cn.com)我们都会想到这样的解法:从前面依次往后推,是val就将该数据后面的元素依次覆盖上来,但是这样的时间复......
  • LeetCode刷题(5)【链表】【环形链表II】(C语言)
    环形链表I​​LeetCode刷题(3)【链表】【环形链表】&扩展_半生瓜のblog-CSDN博客​​环形链表II142.环形链表II-力扣(LeetCode)(leetcode-cn.com)这个题写起来不难,但是证......
  • 【线性表】之栈(C语言)
    栈​​回顾​​​​栈​​​​结构定义​​​​初始化​​​​销毁​​​​入栈​​​​出栈​​​​返回栈顶元素​​​​返回栈中元素个数​​​​判断栈是否为空​​​​......
  • 【线性表】之队列(C语言)
    队列​​队列的概念​​​​结构定义​​​​初始化​​​​销毁​​​​队尾入​​​​队头出​​​​队头出​​​​队头数据​​​​队尾数据​​​​是否为空​​​​返......