码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树各种遍历的递归/迭代写法

时间:2021-04-01 13:41:51      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:tco   void   ems   后序遍历   锯齿形   his   tor   http   public   

一.二叉树的前序遍历

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

技术图片
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        while (null != root || !stack.empty()) {
            while (null != root) {
                ans.add(root.val);
                stack.push(root);
                root = root.left;
            }
            TreeNode node = stack.pop();
            root = node.right;
        }
        return ans;
    }
}
迭代
技术图片
class Solution {

    List<Integer> ans = null;

    public List<Integer> preorderTraversal(TreeNode root) {
        ans = new ArrayList<>();
        dfs(root);
        return ans;
    }

    //根左右
    void dfs(TreeNode root) {
        if (null == root)
            return;
        ans.add(root.val);
        dfs(root.left);
        dfs(root.right);
    }

}
递归

二.二叉树的中序遍历

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

技术图片
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        while (null != root || !stack.empty()) {
            while (null != root) {
                stack.push(root);
                root = root.left;
            }
            TreeNode node = stack.pop();
            ans.add(node.val);
            root = node.right;
        }
        return ans;
    }
}
迭代
技术图片
class Solution {

    List<Integer> ans = null;

    public List<Integer> inorderTraversal(TreeNode root) {
        ans = new ArrayList<>();
        dfs(root);
        return ans;
    }

    void dfs(TreeNode root) {
        if (null == root)
            return;
        dfs(root.left);
        ans.add(root.val);
        dfs(root.right);
    }

}
递归

三.二叉树的后序遍历

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

技术图片
class Solution {

    List<Integer> ans = null;

    public List<Integer> postorderTraversal(TreeNode root) {
        ans = new ArrayList<>();
        dfs(root);
        return ans;
    }

    void dfs(TreeNode root) {
        if (null == root)
            return;
        dfs(root.left);
        dfs(root.right);
        ans.add(root.val);
    }

}
递归
技术图片
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        while (null != root || !stack.empty()) {
            while (null != root) {
                stack.push(root);
                root = root.left;
            }
            TreeNode node = stack.pop();
            if (node.right == null || node.right == pre) {
                ans.add(node.val);
                pre = node;
            } else {
                stack.push(node);
                root = node.right;
            }
        }
        return ans;
    }
}
迭代

四.二叉树的层次遍历

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

技术图片
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        if (null == root)
            return ans;
        Queue<TreeNode> Q = new LinkedList<>();
        Q.add(root);
        while (!Q.isEmpty()) {
            int size = Q.size();
            List<Integer> temp = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = Q.poll();
                temp.add(node.val);
                if (null != node.left)
                    Q.add(node.left);
                if (null != node.right)
                    Q.add(node.right);
            }
            ans.add(temp);
        }
        return ans;
    }
}
迭代

五.二叉树的锯齿形层次遍历

https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/

技术图片
class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        if (null == root)
            return ans;
        Queue<TreeNode> Q = new LinkedList<>();
        Stack<Integer> stack = new Stack<>();
        int flag = 1;
        Q.add(root);
        while (!Q.isEmpty()) {
            int size = Q.size();
            List<Integer> temp = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = Q.poll();
                if (null != node.left)
                    Q.add(node.left);
                if (null != node.right)
                    Q.add(node.right);
                if (flag == 1)
                    temp.add(node.val);
                else
                    stack.push(node.val);
            }
            if (flag == 1)
                ans.add(temp);
            else {
                while (!stack.empty()) 
                    temp.add(stack.pop());
                ans.add(temp);
            }
            flag *= -1;
        }
        return ans;
    }
}
迭代

 

 

二叉树各种遍历的递归/迭代写法

标签:tco   void   ems   后序遍历   锯齿形   his   tor   http   public   

原文地址:https://www.cnblogs.com/yuanweidao/p/14605067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!