leetcode235

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    Stack<TreeNode> SP = new Stack<TreeNode>();
        Stack<TreeNode> SQ = new Stack<TreeNode>();

        bool findP = false;//是否找到p
        bool findQ = false;//是否找到q

        List<TreeNode> listP = new List<TreeNode>();
        List<TreeNode> listQ = new List<TreeNode>();

        private void DFS(TreeNode root, int p, int q)
        {
            if (root != null)
            {
                if (findP && findQ)
                {
                    return;
                }

                if (!findP)
                {
                    SP.Push(root);
                }
                if (!findQ)
                {
                    SQ.Push(root);
                }

                if (root.val == p)
                {
                    //p结点寻找结束
                    findP = true;
                    listP = SP.ToList();
                }
                if (root.val == q)
                {
                    //q结点寻找结束
                    findQ = true;
                    listQ = SQ.ToList();
                }

                if (root.left != null)
                {
                    DFS(root.left, p, q);
                }
                if (root.right != null)
                {
                    DFS(root.right, p, q);
                }

                if (!findP)
                {
                    SP.Pop();
                }
                if (!findQ)
                {
                    SQ.Pop();
                }
            }
        }

        public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
        {
            if (root == null || p == null || q == null)
            {
                return null;
            }
            //采用深度优先搜索,找到p和q
            DFS(root, p.val, q.val);

            for (int i = 0; i < listP.Count; i++)
            {
                for (int j = 0; j < listQ.Count; j++)
                {
                    var nodep = listP[i];
                    var nodeq = listQ[j];
                    if (nodep.val == nodeq.val)
                    {
                        return nodep;
                    }
                }
            }
            return null;
        }
}

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/#/description

时间: 2024-11-06 19:09:27

leetcode235的相关文章

LeetCode235——Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

LeetCode235:Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

leetcode235 - Lowest Common Ancestor of a Binary Search Tree - easy

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has b

算法训练营

概念:算法与数据结构相辅相成 算法是为了解决某一个具体的问题,提出来的一个解法 数据结构是为了支撑这次解法,所提出的一种存储结构 1.两数之和(LeetCode1) 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] =