【每日一题】LeetCode 783.二叉搜索树节点最小距离

本文最后更新于:2023年11月8日 中午

LeetCode 783.二叉搜索树节点最小距离

思路

今天的题都比较无脑,直接无脑dfs搜索最小差值,暴力搜索一遍得出的最后结果就是最小值

C++代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int ans, last;
bool is_first;
int minDiffInBST(TreeNode* root) {
ans=INT_MAX, is_first=true;
dfs(root);
return ans;
}

void dfs(TreeNode* root)
{
if(!root) return;
dfs(root->left);
if(is_first)
{
last=root->val;
is_first=false;
}
else
{
ans=min(ans, root->val-last);
last=root->val;
}
dfs(root->right);
}
};

AcWing 71.二叉树的深度

思路

无脑递归

C++代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 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:
int treeDepth(TreeNode* root) {
if(!root) return 0;
return max(treeDepth(root->left), treeDepth(root->right))+1;
}
};

【每日一题】LeetCode 783.二叉搜索树节点最小距离
https://www.0error.net/2021/04/dbf62d11.html
作者
Jiajun Chen
发布于
2021年4月13日
许可协议