按之字形顺序打印二叉树

  • 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
//使用两个栈可以完成
class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int> > result;
        stack stack1,stack2;
        bool direction = true;//向右打印为true,向左打印为false
        if(pRoot!=NULL)
            stack1.push(pRoot);
        struct TreeNode *node;
        while(!stack1.empty() || !stack2.empty()){
            vector<int> data;
            if(!stack1.empty()){
                while(!stack1.empty()){
                    node = stack1.top();
                    stack1.pop();
                    data.push_back(node->val);
                    if(node->left!=NULL) //先左后右
                        stack2.push(node->left);
                    if(node->right!=NULL)
                        stack2.push(node->right);
                }
                result.push_back(data);
            }
            else if(!stack2.empty()){
                while(!stack2.empty()){
                    node = stack2.top();
                    stack2.pop();
                    data.push_back(node->val);
                    if(node->right!=NULL) //先右后左
                        stack1.push(node->right);
                    if(node->left!=NULL)
                        stack1.push(node->left);
                }
                result.push_back(data);
            }
        }
        return result;
    }
  • 或者另一种方式就是使用计数来计算每一层的节点的数量,然后打印的时候分别按照奇偶行来进行打印即可。代码暂时就不贴了
  • 这一题还有一个变种是只是按照层来打印二叉树,这样其实要简单一点,直接使用queue就可以了,不过要按票一个数来记下每层的节点的数量,在pop queue的时候注意一下就可以了。

  • 下面是第二次写这个题,注意打印的时候只需要判断两个栈哪个是空就可以了:

public class print {
    public ArrayList> print(TreeNode root) {
        int count = 0;
        ArrayList> lists = new ArrayList>();
        if (root == null)
            return lists;
        Stack s1 = new Stack();
        Stack s2 = new Stack();
        s1.add(root);
        while (!(s1.empty() && s2.empty())) {
            ArrayList curr = new ArrayList();
            if (!s1.empty()) {
                while (!s1.empty()) {
                    TreeNode n = s1.pop();
                    curr.add(n.val);
                    if (n.left != null) //从左到右
                        s2.add(n.left);
                    if (n.right != null)
                        s2.add(n.right);
                }
            } else {
                while (!s2.empty()) {
                    TreeNode n = s2.pop();
                    curr.add(n.val);
                    if (n.right != null) //从右到左
                        s1.add(n.right);
                    if (n.left != null)
                        s1.add(n.left);
                }
            }
            lists.add(curr);
        }
        return lists;
    }

    public static void main(String[] args) {
        print p = new print();
        TreeNode root = new TreeNode(1);
        TreeNode l = new TreeNode(2);
        TreeNode r = new TreeNode(3);
        TreeNode ll = new TreeNode(4);
        TreeNode lr = new TreeNode(5);
        TreeNode rl = new TreeNode(6);
        TreeNode rr = new TreeNode(7);
        root.left = l;
        root.right = r;
        root.left.left = ll;
        root.left.right = lr;
        root.right.left = rl;
        root.right.right = rr;
        ArrayList> res = p.print(root);
        for (ArrayList igr : res) {
            for (Integer i : igr) {
                System.out.println(i);
            }
        }
    }
}

class TreeNode {
    TreeNode left;
    TreeNode right;
    int val;

    public TreeNode(int val) {
        this.val = val;
    }

    TreeNode(TreeNode left, TreeNode right, int val) {
        this.left = left;
        this.right = right;
        this.val = val;
    }
}

你可能感兴趣的:(算法)