首页 > 其他分享 >掌握二叉搜索树的双指针 + 公共祖先加深对后序遍历和递归的理解

掌握二叉搜索树的双指针 + 公共祖先加深对后序遍历和递归的理解

时间:2023-01-01 01:55:14浏览次数:38  
标签:遍历 TreeNode cur val 后序 二叉 return null root

530.二叉搜索树的最小绝对差

int min = Integer.MAX_VALUE;
    TreeNode pre;

    /**
     * <A href="https://leetcode.cn/problems/minimum-absolute-difference-in-bst/description/">530. 二叉搜索树的最小绝对差</A>
     */
    public int getMinimumDifference1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        travel1(root);
        return min;
    }

    public void travel1(TreeNode cur) {
        if (cur == null) {
            return;
        }
        travel1(cur.left);
        if (pre != null) {
            min = Math.min(min, (cur.val - pre.val));
        }
        pre = cur;
        travel1(cur.right);
    }

501.二叉搜索树中的众数

    /**
     * <A href="https://leetcode.cn/problems/find-mode-in-binary-search-tree/">501. 二叉搜索树中的众数</A>
     */
    int ModeLength = 0;
    int count = 0;
    List<Integer> arr;
	TreeNode pre;
    public int[] findMode(TreeNode root) {
        arr = new ArrayList<>();
        travel2(root);
        int[] answer = new int[arr.size()];
        for (int i = 0; i < arr.size(); i++) {
            answer[i] = arr.get(i);
        }
        return answer;
    }

    public void travel2(TreeNode cur) {
        if (cur == null) {
            return;
        }
        travel2(cur.left);
        if (pre == null || pre.val != cur.val) {
            count = 1;
        } else {
            count++;
        }
        pre = cur;
        if (count > ModeLength) {
            arr.clear();
            ModeLength = count;
            arr.add(cur.val);
        } else if (count == ModeLength) {
            arr.add(cur.val);
        }
        travel2(cur.right);
    }

236. 二叉树的最近公共祖先

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }
        if(root.val == p.val || root.val == q.val){
            return root;
        }
        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);
        if(left  != null && right != null){
            return root;
        }else if(left==null){
            return right;
        }else{
            return left;
        }
    }

标签:遍历,TreeNode,cur,val,后序,二叉,return,null,root
From: https://www.cnblogs.com/Chain-Tian/p/17017683.html

相关文章

  • day41_0501.二叉搜索树中的众数
    我的思路递归法如果是二叉搜索树如果不是二叉搜索树迭代法我的思路classSolution{private:unordered_map<int,int>map;vector<int>res......
  • day40_0530.二叉搜索树的最小绝对差
    递归1----不用数组递归2------借助数组迭代classSolution{public:TreeNode*pre=NULL;intresult=INT_MAX;voidtraversal(TreeNode*root)......
  • leetcode-563. 二叉树的坡度
    563.二叉树的坡度-力扣(Leetcode)坡度的计算需要4个数左子树所有节点的和右子树所有结点的和左子树的坡度右子树的坡度左子树与右子树节点差值的绝对值为当前节点......
  • 二叉搜索树中第K小的元素
    二叉搜索树中第K小的元素https://leetcode.cn/problems/kth-smallest-element-in-a-bst/解题思路:生序排序,然后找第K个元素BST的中序遍历就是升序排序的结果let......
  • SerializedObject遍历
    # 数据类[Serializable]publicclassTest{publicfloatf;publicVector2v2;}[CreateAssetMenu]publicclassTestAsset:ScriptableObject{[......
  • STL----multiset,平衡二叉数
    《作用》查找,删除,增加节点基本上都是O(logn)多用在比如:vector或一般数组,我们知道如果用这些数据结构要维护一个序列有序,当我们要插入一个数到某个特定的位置那么最坏会......
  • 3任务的创建-列表项的删除&遍历
     1、列表项的删除:从列表中删除指定的列表项,通过uxListRemove()函数来完成pxItemToRemove:要删除的列表项uxListRemove:剩余列表项的数目步骤:获取列表项所在的列表地址将......
  • 三化二叉树trick
    三选一化二叉套路概述这个套路是针对某一建模题的。三选一其实可以扩展到N选一,模型具体如下。发现某种状态可以扩展出\(N\)个状态,且有一个状态相较而言比较特殊(如其他......
  • 迭代(遍历数组)forEach
    1.forEach用法vararr=[13,2,2,5] varsum=0 //forEach用法:Array.forEach(function(数组当前项的值,数组当前项的索引值,数组本身){}) arr.forEach(function(valu......
  • java list集合存储学生对象3种遍历方式
        ......