示例#1
0
        private static void PrintVerticalSumInBinaryTree(BinaryTreeNode binaryTreeNode, DoublyLinkedListNode doublyLinkedListNode)
        {
            if (binaryTreeNode == null)
            {
                return;
            }
            else
            {
                doublyLinkedListNode.SetDoublyLinkedListNodeData(
                    binaryTreeNode.GetBinaryTreeNodeData() + doublyLinkedListNode.GetDoublyLinkedListNodeData());

                if (binaryTreeNode.GetLeft() != null)
                {
                    if (doublyLinkedListNode.GetPreviousNode() == null)
                    {
                        doublyLinkedListNode.SetPreviousNode(
                            new DoublyLinkedListNode(0)
                            );
                        doublyLinkedListNode.GetPreviousNode().
                        SetNextNode(doublyLinkedListNode);
                    }
                    PrintVerticalSumInBinaryTree(binaryTreeNode.GetLeft(),
                                                 doublyLinkedListNode.GetPreviousNode());
                }
                if (binaryTreeNode.GetRight() != null)
                {
                    if (doublyLinkedListNode.GetNextNode() == null)
                    {
                        doublyLinkedListNode.SetNextNode(
                            new DoublyLinkedListNode(0)
                            );
                        doublyLinkedListNode.GetNextNode()
                        .SetPreviousNode(doublyLinkedListNode);
                    }
                    PrintVerticalSumInBinaryTree(binaryTreeNode.GetRight(),
                                                 doublyLinkedListNode.GetNextNode());
                }
            }
        }
示例#2
0
        private static void PrintVerticalSumInBinaryTree(DoublyLinkedListNode doublyLinkedListNode)
        {
            while (doublyLinkedListNode != null &&
                   doublyLinkedListNode.GetPreviousNode() != null)
            {
                doublyLinkedListNode = doublyLinkedListNode.GetPreviousNode();
            }

            while (doublyLinkedListNode != null)
            {
                Console.Write(doublyLinkedListNode.GetDoublyLinkedListNodeData()
                              + " ");
                doublyLinkedListNode = doublyLinkedListNode.GetNextNode();
            }
        }