zoukankan      html  css  js  c++  java
  • 按之字形顺序打印二叉树-剑指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;
        }
    
    }
  • 相关阅读:
    C#实战Microsoft Messaging Queue(MSMQ)消息队列(干货)
    实现动态的XML文件读写操作(依然带干货)
    多线程下访问控件的方式(您一定会用到,附源码啦!)
    Microsoft.VisualBasic.dll的妙用(开发中肯定会用到哦)
    vue使用element-ui的el-input监听不了键盘事件解决
    vue强制刷新组件
    asp.net微信公众平台本地调试设置
    武大女硕士面试被拒,改简历冒充本科生找工作的感想(原创)
    完整的站内搜索Demo(Lucene.Net+盘古分词)
    ASP.NET多线程下使用HttpContext.Current为null解决方案
  • 原文地址:https://www.cnblogs.com/rosending/p/5721721.html
Copyright © 2011-2022 走看看