C# Code Caprice Algorithm Training Camp day21|The minimum absolute difference of a binary search tree, the mode in a binary search tree, and the nearest common ancestor of a binary tree

LeetCode530 Minimum absolute difference of binary search tree

topic:

Given the root node root of a binary search tree, return the minimum difference between any two different node values ​​in the tree.
Difference is a positive number whose value is equal to the absolute value of the difference between two values.

Example 1:
Input: root = [4,2,6,1,3]
Output: 1

Example 2:
Input: root = [1,0,48,null,null,12,49]
Output: 1

Code:
//DFS、中序遍历
public class Solution {
    
    
    public int GetMinimumDifference(TreeNode root) {
    
    
        List<int> list = new List<int>();
        int min = int.MaxValue;
        DFS(root, list);
        for (int i = 0; i < list.Count - 1; i++)
        {
    
    
            min = Math.Min(min, list[i + 1] - list[i]);
        }
        return min;
    }

    public void DFS(TreeNode root, List<int> res)
    {
    
    
        if (root == null) return;
        DFS(root.left, res);
        res.Add(root.val);
        DFS(root.right, res);
    }
}

LeetCode501 Mode in Binary Search Tree

topic:

Given the root node root of a binary search tree (BST) containing repeated values, find and return all modes (ie, the elements with the highest frequency) in the BST.
If there is more than one mode in the tree, they can be returned in any order.

Assume that BST satisfies the following definition:
The value of the node contained in the left subtree of the node is less than or equal to the value of the current node.
The value of the node contained in the right subtree of the node is greater than or equal to the value of the current node. Both
the left subtree and the right subtree are two. cross search tree

Example 1:
Input: root = [1,null,2,2]
Output: [2]

Example 2:
Input: root = [0]
Output: [0]

Code:
//DFS
public class Solution {
    
    
    int count;
    int maxCount;
    int ba;
    List<int> list = new List<int>();
    public int[] FindMode(TreeNode root)
    {
    
    
        DFS(root);
        return list.ToArray();
    }

    public void DFS(TreeNode root)
    {
    
    
        if (root == null) return;
        DFS(root.left);
        Update(root.val);
        DFS(root.right);
    }

    public void Update(int val)
    {
    
    
        if (val == ba)
        {
    
    
            count++;
        }
        else
        {
    
    
            count = 1;
            ba = val;
        }
        if (count == maxCount)
        {
    
    
            list.Add(ba);
        }
        if (count > maxCount)
        {
    
    
            maxCount = count;
            list.Clear();
            list.Add(ba);
        }
    }
}

LeetCode236 nearest common ancestor of binary tree

topic:

Given a binary tree, find the nearest common ancestor of two specified nodes in the tree.
The definition of the nearest common ancestor in Baidu Encyclopedia is: "For two nodes p and q of a rooted tree T, the nearest common ancestor is represented as a node x, such that x is the ancestor of p and q and the depth of x is as large as possible (a A node can also be its own ancestor)."

Example 1:
Insert image description here

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The nearest common ancestor of node 5 and node 1 is Node 3.

Example 2:
Insert image description here

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The nearest common ancestor of node 5 and node 4 is Node 5. Because by definition the nearest common ancestor node can be the node itself.

Example 3:
Input: root = [1,2], p = 1, q = 2
Output: 1

Code:
//DFS
public class Solution {
    
    
    public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    
    
        if (root == null || root == p || root == q)
        {
    
    
            return root;
        }
        //后续遍历 左右中
        TreeNode left = LowestCommonAncestor(root.left, p, q);
        TreeNode right = LowestCommonAncestor(root.right, p, q);
        if (left == null && right == null)  //未找到节点p、q
        {
    
    
            return null;
        }
        else if (left == null && right != null)    //找到一个节点
        {
    
    
            return right;
        }
        else if (left != null && right == null)
        {
    
    
            return left;
        }
        else
        {
    
    
            return root;
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_44740741/article/details/129944854
Recommended