栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

leetcode

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode

假期了继续刷刷leetcode吧!

前序,中序,后序递归实现
class Solution {
    public List preorderTraversal(TreeNode root) {
        List ans  = new ArrayList();
        preorder(root,ans);
        return ans;
    }
    public void preorder(TreeNode root, List ans)
    {
        if(root==null)  return ;
        ans.add(root.val);
        preorder(root.left,ans);
        preorder(root.right,ans);
    }
}
class Solution {
    public List inorderTraversal(TreeNode root) {
        List ans =new ArrayList();
        inorder(root,ans);
        return ans;
    }
    public void inorder(TreeNode root,List ans)
    {
        if(root==null)  return;
        inorder(root.left,ans);
        ans.add(root.val);
        inorder(root.right,ans);

    }
}
class Solution {
    public List postorderTraversal(TreeNode root) {
        List ans  = new ArrayList();
        postorder(root,ans);
        return ans;
    }
    public void postorder(TreeNode root, List ans)
    {
        if(root==null)  return;
        postorder(root.left,ans);
        postorder(root.right,ans);
        ans.add(root.val);
    }
}
非递归实现

1.前序遍历
以上图为例,1入栈,往左走,2入栈,4入栈,4出栈(此时root==null 但仍能进入外层循环,因为栈不空,跳过内层循环,2出栈,往右走 5入栈,5出栈,1出栈,3入栈,6入栈,6出,3出
因此前序遍历序列425163

class Solution {
    public List preorderTraversal(TreeNode root) {
        List ans = new ArrayList();
        Deque stack = new linkedList();
        while (root != null||!stack.isEmpty()) {
            while (root != null) {
                ans.add(root.val);
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            root = root.right;
        }
        return ans;
    }
}

2.中序遍历

class Solution {
    public List inorderTraversal(TreeNode root) {
        List ans = new ArrayList();
        Deque stack = new linkedList();
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            ans.add(root.val);
            root = root.right;
        }
        return ans;
    }
}
  1. 后序遍历
class Solution {
    public List postorderTraversal(TreeNode root) {
        List ans = new ArrayList();
        Deque stack = new linkedList();
        while (root != null||!stack.isEmpty()) {
            while (root != null) {
                ans.add(root.val);
                stack.push(root);
                root = root.right;
            }
            root = stack.pop();
            root = root.left;
        }
        Collections.reverse(ans);
        return ans;
    }
}
层序遍历

首先层序遍历即基于BFS

void bfs(TreeNode root) {
    Queue queue = new ArrayDeque<>();
    queue.add(root);
    while (!queue.isEmpty()) {
        TreeNode node = queue.poll(); 
        if (node.left != null) {
            queue.add(node.left);
        }
        if (node.right != null) {
            queue.add(node.right);
        }
    }
}

ArrayDeque 是 Java 集合中双端队列的数组实现,linkedList是双端队列的链表实现
在每一层遍历开始前,先记录队列中的结点数量 n(也就是这一层的结点数量),然后一口气处理完这一层的 n 个结点。

class Solution{
    public List> levelOrder(TreeNode root) {
        List> ans = new ArrayList<>();
        Queue queue = new ArrayDeque<>();
        if (root != null)   queue.add(root);
        while (!queue.isEmpty()) {
            int n = queue.size();//第一层size=1
            List level = new ArrayList<>();
            //分层加入,
            for (int i = 0; i < n; i++) //第一层就循环2次,即把第二层的结点入队
            { 
                TreeNode node = queue.poll();//先把根结点出队
                level.add(node.val);
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            ans.add(level);
        }
        return ans;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/686316.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号