代码随想录算法训练营第二十一天| 530.二叉搜索树的最小绝对差, 501.二叉搜索树中的众数, 236. 二叉树的最近公共祖先

530. Minimum Absolute Difference in BST

  • 思路
    • 中序遍历递归
    • java
      
      class Solution {
          int res = Integer.MAX_VALUE;
          TreeNode pre = null;
          public int getMinimumDifference(TreeNode root) {
              if ( root == null){
                  return 0;
              }
              
              helper(root);
              return res;
          }
          private void helper( TreeNode root){
              if (root == null){
                  return;
              }
              
              helper(root.left);
              
              if (pre != null){
                  res = Math.min (res, root.val - pre.val);
              }
              pre = root;
              
              helper(root.right);
         
          }    
       
      }
      
      
    • 迭代
    • java
      class Solution {
          TreeNode pre;
          Stack stack;
          
          public int getMinimumDifference(TreeNode root) {
              if (root == null){
                  return 0;
              }
              stack = new Stack<>();
              TreeNode temp = root;
              int res = Integer.MAX_VALUE;
              while( temp != null || !stack.isEmpty()){
                  if ( temp != null){
                      stack.push(temp);
                      temp = temp.left;                
                  }else{
                      temp = stack.pop();
                      if(pre != null){
                          res = Math.min(res, temp.val - pre.val);
                      }
                      pre = temp;
                      temp = temp.right;
                  }
              }  
              return res;       
          }
      }

 501. Find Mode in Binary Search Tree

  • 思路
    • BST就是中序遍历
    • java
      
      
      class Solution {
          List res;
          Integer maxCount;
          Integer count;
          TreeNode pre;
          public int[] findMode(TreeNode root) {
              res = new ArrayList<>();
              maxCount = 0;
              count = 0;
              helper(root);
              int [] result = new int[res.size()];
              for(int i = 0; i < result.length; i++){
                  result[i] = res.get(i);
              }
              return result;
          }
          private void helper(TreeNode root){
              if (root == null){
                  return;
              }
              helper(root.left);
              
              if(pre != null && root.val == pre.val){
                  count ++;
              }else{
                  count = 1;
              }
              
              if (count > maxCount){
                  maxCount = count;
                  res.clear();
                  res.add(root.val);
              }else if (count == maxCount){
                  res.add(root.val);
              }
              pre = root;
              
              helper(root.right);
          }
          
      }
      
      

 236. Lowest Common Ancestor of a Binary Tree

  • 思路
    • java
      class Solution {
          public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
              return helper(root, p, q);
          }
          
          private TreeNode helper( TreeNode root, TreeNode p, TreeNode q){
              if (root == null){
                  return root;
              }
              
              if(root == p || root == q){
                  return root;
              }
              
              TreeNode left = helper( root.left, p, q);
              TreeNode right = helper(root.right, p, q);
              
              if (left != null && right != null){
                  return root;
              }
              
              if (left == null && right != null){
                  return right;
              }
              else if (right == null && left !=null){
                  return left;
              }else{
                  return null;
              }
              
          }
      }
      
      

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值