【文件属性】:
文件名称:LeetCode判断字符串是否循环-leetcode:leetcode编程题练习
文件大小:12KB
文件格式:ZIP
更新时间:2021-07-01 06:00:06
系统开源
LeetCode判断字符串是否循环
leetcode
编程题
538.
代码思路:反序中序遍历的方法
BST的中序遍历就是从小到大,那么反过来就是从大到小,然后累加就好了.
/**
*
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
{
int
num=0;
public:
TreeNode*
convertBST(TreeNode*
root)
{
if(root!=NULL){
//遍历右子树
convertBST(root->right);
//遍历节点
root->val=root->val+num;
num=root->val;
//遍历左子树
convertBST(root->left);
return
root;
}
return
NULL;
}
};
时间复杂度
【文件预览】:
leetcode-master
----leetcode.md(18KB)
----readme.md(18KB)