• 二叉树的迭代遍历


    二叉树的迭代遍历

    后序遍历

    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<>(); 
            if (root == null) return res;
            Stack<TreeNode> stack = new Stack<>();   
            stack.push(root);  
            while (!stack.isEmpty()) {
                TreeNode t = stack.pop();   
                if (t != null) {   
                    stack.push(t);   
                    stack.push(null);  
                    if (t.right != null) stack.push(t.right);  
                    if (t.left != null)  stack.push(t.left); 
                } else {  
                    res.add(stack.pop().val);   
                }
            }
            return res;
        }
    }
    

    前序遍历

    class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<>(); 
            if (root == null) return res;
            Stack<TreeNode> stack = new Stack<>();   
            stack.push(root);  
            while (!stack.isEmpty()) {
                TreeNode t = stack.pop();   
                if (t != null) {   
                    if (t.right != null) stack.push(t.right);  //
                    if (t.left != null)  stack.push(t.left); //
                    stack.push(t);   //
                    stack.push(null);  //
                } else {  
                    res.add(stack.pop().val);   
                }
            }
            return res;
        }
    }
    

    中序遍历

    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<>(); 
            if (root == null) return res;
            Stack<TreeNode> stack = new Stack<>();   
            stack.push(root);  
            while (!stack.isEmpty()) {
                TreeNode t = stack.pop();   
                if (t != null) {  
                    if (t.right != null) stack.push(t.right);   
                    stack.push(t);   
                    stack.push(null);   
                    if (t.left != null)  stack.push(t.left); 
                } else {  
                    res.add(stack.pop().val);   
                }
            }
            return res;
        }
    }
    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<Integer>();
            Deque<TreeNode> stk = new LinkedList<TreeNode>();
            while (root != null || !stk.isEmpty()) {
                while (root != null) {
                    stk.push(root);
                    root = root.left;
                }
                root = stk.pop();
                res.add(root.val);
                root = root.right;
            }
            return res;
        }
    }
    
    

    层次遍历

    class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> res=new ArrayList<List<Integer>>();
            if(root==null) return res;
            Queue<TreeNode> queue=new LinkedList<>();
            queue.add(root);
            while(!queue.isEmpty()){
                List<Integer> list=new ArrayList<>();
                int size=queue.size();
                for(int i=0;i<size;i++){
                    TreeNode node=queue.poll();
                    list.add(node.val);
                    if(node.left!=null) queue.add(node.left);
                    if(node.right!=null) queue.add(node.right);
                }
                res.add(list);
            }
            return res;
        }
    }
    

    未经作者同意请勿转载

    本文来自博客园作者:aixueforever,原文链接:https://www.cnblogs.com/aslanvon/p/13631838.html

  • 相关阅读:
    Android中连接蓝牙设备时遇到createRfcommSocketToServiceRecord的UUID问题和BluetoothSocket的connect失败
    android4.0蓝牙使能的详细解析 (转载)
    蓝牙介绍
    Bluetooth 4.0之Android 讲解
    jQuery来源学习笔记:扩展的实用功能
    Linux 0.12 内核管理存储器
    java战斗系列-战斗MAVENPW结构
    牟大哥:《App自我促销》连载2 直立人迁移走
    AC自己主动机 总结
    SpringMVC 上下文webApplicationContext
  • 原文地址:https://www.cnblogs.com/aslanvon/p/13631838.html
Copyright © 2020-2023  润新知