• 按之字形顺序打印二叉树-剑指Offer


    按之字形顺序打印二叉树

    题目描述

    请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

    思路

    1. 根据题意,每行的节点的访问顺序是相反的,我们可以用两个栈来隔行存储,一个栈中根据“左结点->右结点”的顺序访问另一个栈的栈顶元素,而另一个栈根据“右子树->左子树”的顺序访问另一个栈的栈顶元素,直到两个栈都为空

    代码

    import java.util.ArrayList;
    import java.util.Stack;
    /*
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
    
        public TreeNode(int val) {
            this.val = val;
    
        }
    
    }
    */
    public class Solution {
        public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
    		ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
            if (pRoot == null) {
                return result;
            }
            Stack<TreeNode> stack1 = new Stack<TreeNode>();
            Stack<TreeNode> stack2 = new Stack<TreeNode>();
            ArrayList<Integer> list = new ArrayList<Integer>();
            list.add(pRoot.val);
            result.add(list);
            stack1.push(pRoot);
            while (stack1.isEmpty() || stack2.isEmpty()) {
                if (stack1.isEmpty() && stack2.isEmpty()) {
                    break;
                }
                ArrayList<Integer> temp = new ArrayList<Integer>();
                if (stack2.isEmpty()) {
                    while (!stack1.isEmpty()) {
                        if (stack1.peek().right != null) {
                            temp.add(stack1.peek().right.val);
                            stack2.push(stack1.peek().right);
                        }
                        if (stack1.peek().left != null) {
                            temp.add(stack1.peek().left.val);
                            stack2.push(stack1.peek().left);
                        }
                        stack1.pop();
                    }
                } else {
                    while (!stack2.isEmpty()) {
                        if (stack2.peek().left != null) {
                            temp.add(stack2.peek().left.val);
                            stack1.push(stack2.peek().left);
                        }
                        if (stack2.peek().right != null) {
                            temp.add(stack2.peek().right.val);
                            stack1.push(stack2.peek().right);
                        }
                        stack2.pop();
                    }
                }
                if (temp.size() > 0) {
                    result.add(temp);
                }
            }
            return result;
        }
    
    }

    按之字形顺序打印二叉树

    题目描述

    请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

    思路

    1. 根据题意,每行的节点的访问顺序是相反的,我们可以用两个栈来隔行存储,一个栈中根据“左结点->右结点”的顺序访问另一个栈的栈顶元素,而另一个栈根据“右子树->左子树”的顺序访问另一个栈的栈顶元素,直到两个栈都为空

    代码

    import java.util.ArrayList;
    import java.util.Stack;
    /*
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
    
        public TreeNode(int val) {
            this.val = val;
    
        }
    
    }
    */
    public class Solution {
        public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
    		ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
            if (pRoot == null) {
                return result;
            }
            Stack<TreeNode> stack1 = new Stack<TreeNode>();
            Stack<TreeNode> stack2 = new Stack<TreeNode>();
            ArrayList<Integer> list = new ArrayList<Integer>();
            list.add(pRoot.val);
            result.add(list);
            stack1.push(pRoot);
            while (stack1.isEmpty() || stack2.isEmpty()) {
                if (stack1.isEmpty() && stack2.isEmpty()) {
                    break;
                }
                ArrayList<Integer> temp = new ArrayList<Integer>();
                if (stack2.isEmpty()) {
                    while (!stack1.isEmpty()) {
                        if (stack1.peek().right != null) {
                            temp.add(stack1.peek().right.val);
                            stack2.push(stack1.peek().right);
                        }
                        if (stack1.peek().left != null) {
                            temp.add(stack1.peek().left.val);
                            stack2.push(stack1.peek().left);
                        }
                        stack1.pop();
                    }
                } else {
                    while (!stack2.isEmpty()) {
                        if (stack2.peek().left != null) {
                            temp.add(stack2.peek().left.val);
                            stack1.push(stack2.peek().left);
                        }
                        if (stack2.peek().right != null) {
                            temp.add(stack2.peek().right.val);
                            stack1.push(stack2.peek().right);
                        }
                        stack2.pop();
                    }
                }
                if (temp.size() > 0) {
                    result.add(temp);
                }
            }
            return result;
        }
    
    }
  • 相关阅读:
    关于数组添加元素的优化 __原文司徒正美
    FIRST
    二维数组作为参数传递问题
    c++线程池的实现
    阻塞和非阻塞
    N皇后解决方法
    判断一棵二叉树是否为BST,一棵树是否为完全二叉树
    c++11可变参数的使用
    最大似然估计和最大后验估计
    利用Microsoft Azure Machine Learning Studio创建机器学习实例
  • 原文地址:https://www.cnblogs.com/rosending/p/5721721.html
Copyright © 2020-2023  润新知