• leetcode_二叉树的前序,中序,后序遍历实现


    leetcode_二叉树的前序,中序,后序遍历实现

    前序遍历

    https://leetcode-cn.com/problems/binary-tree-preorder-traversal/

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
                    LinkedList<TreeNode> stack=new LinkedList<>();
            LinkedList<Integer> out=new LinkedList<>();
             if(root==null){
                 return out;
             }
             stack.add(root);
             while(!stack.isEmpty()){
                TreeNode last = stack.pollLast();
                out.add(last.val);
                if(last.right!=null){
                    stack.add(last.right);
                }
                 if(last.left!=null){
                    stack.add(last.left);
                }
             }
             return out;
        }
    }

    中序遍历

    https://leetcode-cn.com/problems/binary-tree-inorder-traversal/

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
                Stack<TreeNode> stack=new Stack<>();
                LinkedList<Integer> out=new LinkedList<>();
                TreeNode curr=root;
                while(!stack.isEmpty()|| curr !=null){
                    while(curr!=null){
                        stack.push(curr);
                        curr=curr.left;
                    }
                    curr=stack.pop();
                    out.add(curr.val);
                    curr=curr.right;
                }
                return out;
        }
    }

    后序遍历

    https://leetcode-cn.com/problems/binary-tree-postorder-traversal/

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            LinkedList<TreeNode> stack=new LinkedList<>();
             LinkedList<Integer> out=new LinkedList<>();
             if(root==null){
                 return out;
             }
             stack.add(root);
             while(!stack.isEmpty()){
                TreeNode last=stack.pollLast();
                 out.addFirst(last.val);
                 if(last.left!=null){
                    stack.add(last.left); 
                 }
                 if(last.right!=null){
                    stack.add(last.right); 
                 }
             }
             return out;
        }
    }
  • 相关阅读:
    Ajax 异步请求返回集合遍历问题
    JS 类数组,字符串,转换成数组的方法
    laravel、TP、YII三个框架的优缺点对比
    纵深防御
    渗透测试小结
    常见的设计模式
    CDN简介
    WAF小介
    分布式事务及其常见的解决方案
    redis主从复制
  • 原文地址:https://www.cnblogs.com/liran123/p/12810315.html
Copyright © 2020-2023  润新知