【问题标题】:How to traverse a binary tree in left right direction?如何在左右方向遍历二叉树?
【发布时间】:2015-07-03 19:35:36
【问题描述】:

我想要一个解决方案,在二叉树中以交替的左右方向遍历。水平顺序遍历是“1 2 3 4 5 6 7”。在所需条件下,我的输出必须为“1 2 3 7 6 5 4”。下面是我写的代码。这是不正确的。

public void traverseLevel(BinaryTreeNode root) {
    Queue<BinaryTreeNode> q = new LinkedList<BinaryTreeNode>();
    q.add(root);
    int c = 0;
    while (!q.isEmpty()) {

        root = q.poll();
        System.out.print(root.getData() + " ");
        if (c == 0) {
            c = 1;
            if (root.getLeft() != null) {
                q.add(root.getLeft());
            }
            if (root.getRight() != null) {
                q.add(root.getRight());
            }

        } else {
            c = 0;
            if (root.getRight() != null) {
                q.add(root.getRight());

            }
            if (root.getLeft() != null) {
                q.add(root.getLeft());
            }
        }
    }
}

得到的输出是“1 2 3 5 4 6 7”

【问题讨论】:

    标签: java data-structures binary-tree


    【解决方案1】:

    我从你的问题中了解到你想以螺旋顺序打印树。

    你可以简单地使用两个堆栈

    1. 从左到右打印的第一个堆栈。
    2. 从右到左打印第二叠。

    从根节点开始,您必须将子节点存储在一个堆栈中。 因此,对于每个迭代,打印节点都存在于一个堆栈中。下一级被压入另一个堆栈。

    【讨论】:

      【解决方案2】:

      我会为你跟踪你的实现。首先,您的队列包含节点 1。

      1

      然后你将它的子元素插入队列,因为 c 为 0,所以先将左边的子元素插入队列中。

      1 2 3

      1 然后退出队列,所以你留下了

      2 3

      由于您想从现在开始,3 将是理想的起点。但是,2 在您的队列中排在第一位,因此它的孩子将首先被推送。

      2 3 5 4,然后2退出形成3 5 4

      此时 3 会将其子节点推入队列,因为 c 为 0,所以将左侧排在最前面。

      3 5 4 6 7

      我认为您想要做的是拥有一个队列和一个堆栈,其中队列包含要打印的项目,而堆栈接收从队列中删除的项目。然后,您可以弹出堆栈中的每个项目并将其子项推入队列。

      例如,我们从队列中的 1 和一个空堆栈开始。然后我们从队列中删除 1,打印它,然后将它压入堆栈。然后我们弹出 1 并将其子节点从左到右插入队列中。

      2 3

      接下来,我们打印 2 并将其放入堆栈。与 3 相同。现在我们的堆栈包含

      2 3

      然后,我们从堆栈中弹出 3 并将其子项排队,这一次将其排在最前面。然后我们弹出 2 并对它的孩子做同样的事情。我们的队列现在包含

      7 6 5 4

      然后重复这个过程,直到你到达所有的叶子。

      另外,一个提示:c 可以是布尔值而不是数字,因为您只使用 0 和 1 作为值。这样可以节省空间。 :)

      【讨论】:

        【解决方案3】:

        以下方法是在 C++ 中使用 STL

        struct TreeNode // basic Tree Structure
        {
        int data;
        TreeNode* leftchild;
        TreeNode* rightchild;
        }
        void alternateLeftRight(TreeNode*root)
        {
        Stack<TreeNode*>currentLevel; // take two stacks 
        Stack<TreeNode*>nextLevel;
        currentLevel.push(root);
        int LeftToRight=0;  // LeftToRight mean we will approach leftchild before rightchild, if LeftToRight=0 , in this case we will approach rightchild first
        while(!currentLevel.empty())
        { 
        
         TreeNode*temp=currentLevel.top();
         cout<<temp->data<<" ";
         currentLevel.pop();
        if(LeftToRight)
        {
         if(temp->leftchild)nextLevel.push(leftchild);
         if(temp->rightchild)nextLevel.push(rightchild);
        }
        else
        {
          if(temp->leftchild)nextLevel.push(rightchild);
          if(temp->rightchild)nextLevel.push(leftchild);
        }
        
        if(currentLevel.empty())
        {
        swap(currentLevel,nextLevel);
        LeftToRight=1-LeftToRight;
        }
        
        }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-01
          • 2022-11-11
          • 2021-01-12
          相关资源
          最近更新 更多