コード例 #1
0
ファイル: TreeUtility.cs プロジェクト: Giruba/LCAOfBinaryTree
        private static Result GetLowestCommonAncestor(BinaryTreeNode treeNode, int value1, int value2)
        {
            if (treeNode == null)
            {
                return(new Result());
            }

            Result result = new Result();

            if (treeNode.GetData() == value1)
            {
                result.presenceOfNode1 = true;
                result.treeNode        = treeNode;
                return(result);
            }

            if (treeNode.GetData() == value2)
            {
                result.presenceOfNode2 = true;
                result.treeNode        = treeNode;
                return(result);
            }

            Result leftResult  = GetLowestCommonAncestor(treeNode.GetLeft(), value1, value2);
            Result rightResult = GetLowestCommonAncestor(treeNode.GetRight(), value1, value2);

            if (leftResult.treeNode != null && rightResult.treeNode != null)
            {
                result.presenceOfNode2 = true;
                result.presenceOfNode1 = true;
                result.treeNode        = treeNode;
                return(result);
            }


            if (leftResult.treeNode != null)
            {
                result.presenceOfNode1 = true;
                result.treeNode        = leftResult.treeNode;
                return(result);
            }

            result.presenceOfNode2 = true;
            result.treeNode        = rightResult.treeNode;
            return(result);
        }
コード例 #2
0
ファイル: TreeUtility.cs プロジェクト: Giruba/LCAOfBinaryTree
        public static bool isPresent(BinaryTreeNode binaryTreeNode, int value)
        {
            if (binaryTreeNode == null)
            {
                return(false);
            }

            if (binaryTreeNode.GetData() == value || isPresent(binaryTreeNode.GetLeft(), value) ||
                isPresent(binaryTreeNode.GetRight(), value))
            {
                return(true);
            }

            return(false);
        }