Leetcode树专题

目录

Leetcode098(验证二叉搜索树)-------中序遍历

地址:

描述:

代码:

Leetcode094(二叉树中序遍历)

题解:

Leetcode101(对称二叉树)————递归、迭代法

题解:

描述:

Leetcode105根据前序和中序遍历构造二叉树----递归思想

题解:

描述:

Leetcode102二叉树的层序遍历----队列运用

描述:

题解:

Leetcode236(二叉树最近的公共祖先)--------递归

描述:

题解:

Leetcode104二叉树的最大深度------(进阶:二叉树的直径)-----递归解法

描述:

题解:

Leetcode534(二叉树的直径)---------递归

描述:

题解:

Leetcode124(二叉树的最大路径之和)-------递归

描述:

题解:

Leetcode173(中序遍历二叉搜索迭代器)(迭代法)--------栈

描述:

题解:


Leetcode098(验证二叉搜索树)-------中序遍历

地址:

https://leetcode-cn.com/problems/validate-binary-search-tree/

描述:

代码:

/**
 * 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:
    bool isValidBST(TreeNode* root) {
      stack <TreeNode*> it;
      long long pre=(long long)INT_MIN-1;
      while(root!=nullptr||it.size()){
          //把所有左子树全部压入栈
          while(root!=nullptr)
          { 
          it.push(root);
          root=root->left;
          }
      root=it.top();
      it.pop();
      //如果当前节点的值<=pre说明不满足递增,返回false;
      if(root->val<=pre){
          return false;
      }
      //将pre的值更新
      pre=root->val;
      //判断有没有右子树,如果有也压入栈
      root=root->right;
      }
      return true;
    }
};

Leetcode094(二叉树中序遍历)

题解:

https://blog.csdn.net/qq_52934831/article/details/119121225

Leetcode101(对称二叉树)————递归、迭代法

题解:

https://blog.csdn.net/qq_52934831/article/details/119809050

描述:

Leetcode105根据前序和中序遍历构造二叉树----递归思想

题解:

https://blog.csdn.net/qq_52934831/article/details/119820894

描述:

Leetcode102二叉树的层序遍历----队列运用

描述:

题解:

https://blog.csdn.net/qq_52934831/article/details/119830592

Leetcode236(二叉树最近的公共祖先)--------递归

描述:

题解:

https://blog.csdn.net/qq_52934831/article/details/119851569

Leetcode104二叉树的最大深度------(进阶:二叉树的直径)-----递归解法

描述:

题解:

https://blog.csdn.net/qq_52934831/article/details/119858866

Leetcode534(二叉树的直径)---------递归

描述:

题解:

https://blog.csdn.net/qq_52934831/article/details/119858965

Leetcode124(二叉树的最大路径之和)-------递归

描述:

 

题解:

https://blog.csdn.net/qq_52934831/article/details/119862113

Leetcode173(中序遍历二叉搜索迭代器)(迭代法)--------栈

描述:

题解:

https://blog.csdn.net/qq_52934831/article/details/119864963

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值