Beispiel #1
0
        public BiNode <T> FindLCA(BiNode <T> root, BiNode <T> target1, BiNode <T> target2)
        {
            if (root == null)
            {
                return(null);
            }
            if (root == target1 || root == target2)
            {
                return(root);
            }

            BiNode <T> left  = FindLCA(root.LeftChild, target1, target2);
            BiNode <T> right = FindLCA(root.RightChild, target1, target2);

            if (left != null && right != null)
            {
                return(root);
            }

            return(left != null ? left : right);
        }
Beispiel #2
0
        public int FindLevel(BiNode <T> root, BiNode <T> target)
        {
            if (root == null)
            {
                return(-1);
            }
            if (root == target)
            {
                return(0);
            }

            int level = FindLevel(root.LeftChild, target);  // 先在左子树找

            if (level == -1)
            {
                level = FindLevel(root.RightChild, target); // 如果左子树没找到,在右子树找
            }
            if (level != -1)                                // 找到了,回溯
            {
                return(level + 1);
            }

            return(-1);  // 如果左右子树都没找到
        }
Beispiel #3
0
        public BiTree(T data, BiNode <T> leftBiNode, BiNode <T> rightBiNode)
        {
            BiNode <T> BiNode = new BiNode <T>(data, leftBiNode, rightBiNode);

            head = BiNode;
        }
Beispiel #4
0
        public BiTree(T data)
        {
            BiNode <T> BiNode = new BiNode <T>(data);

            head = BiNode;
        }
Beispiel #5
0
 public BiTree()
 {
     head = null;
 }