随想录刷题笔记 —二叉树篇2 429N叉树层序遍历 515每个树行找最大值 116填充每个节点的下一个右侧节点指针

429N叉树层序遍历

跟二叉树层序遍历基本相同,区别在于需要使用for循环遍历孩子集合list

解法一:使用递归

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        if (root == null){
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        ndfs(root, 0, res);
        return res;
    }

    public void ndfs(Node root, int deep, List<List<Integer>> res) {
        if (res.size()<=deep){
            List<Integer> list = new ArrayList<>();
            list.add(root.val);
            res.add(list);
        }else {
            res.get(deep).add(root.val);
        }
        for (int i = 0; i < root.children.size(); i++) {
            ndfs(root.children.get(i), deep+1, res);
        }
    }
}

解法一:使用队列

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        if (root == null){
            return new ArrayList<>();
        }
        Queue<Node> nodes = new LinkedList<>();
        nodes.offer(root);
        int count = 1;
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> templist = new ArrayList<>();
        while (!nodes.isEmpty()){
            Node temp = nodes.poll();
            templist.add(temp.val);
            count--;
            for (int i = 0; i < temp.children.size(); i++) {
                nodes.offer(temp.children.get(i));
            }
            if (count==0){
                result.add(new ArrayList<>(templist));
                templist.clear();
                count = nodes.size();
            }
        }
        return result;
    }
}

515每个树行找最大值

层序遍历,遍历每个结点时比较最大值,再该层结点遍历结束后存储该层最大值

解法一:递归

class Solution {
    public List<Integer> largestValues(TreeNode root){
        if (root == null){
            return new ArrayList<>();
        }
        List<Integer> res = new ArrayList<>();
        dfs1(root, 0, res);
        return res;
    }

    public void dfs1(TreeNode root, int deep, List<Integer> res) {
        if (res.size()<=deep){
            res.add(root.val);
        }else {
            res.set(deep, Math.max(root.val, res.get(deep)));
        }
        if (root.left!=null){
            dfs1(root.left, deep+1, res);
        }
        if (root.right!=null){
            dfs1(root.right, deep+1, res);
        }
    }
}

解法二:队列

class Solution {
    public List<Integer> largestValues(TreeNode root) {
        if (root == null){
            return new ArrayList<>();
        }
        Queue<TreeNode> nodes = new LinkedList<>();
        nodes.offer(root);
        int count = 1;
        int n = -2147483648;
        List<Integer> result = new ArrayList<>();
        while (!nodes.isEmpty()){
            TreeNode temp = nodes.poll();
            n = Math.max(n, temp.val);
            count--;
            if (temp.left!=null){
                nodes.offer(temp.left);
            }
            if (temp.right!=null){
                nodes.offer(temp.right);
            }
            if (count==0){
                result.add(n);
                n = -2147483648;
                count = nodes.size();
            }
        }
        return result;
    }
}

116填充每个节点的下一个右侧节点指针

解法一:队列

层次遍历,使用node1和node2用于next赋值

需要判断每层第一个元素——即node1为空

每层最后一个元素——count=0

class Solution {
    public Node connect(Node root) {
        if (root == null){
            return root;
        }
        Queue<Node> nodes = new LinkedList<>();
        nodes.offer(root);
        int count = 1;
        Node node1 = null;
        Node node2 = null;
        while (!nodes.isEmpty()){
            node2 = nodes.poll();
            count--;
            if (node2.left!=null){
                nodes.offer(node2.left);
            }
            if (node2.right!=null){
                nodes.offer(node2.right);
            }
            if (node1==null){
                node1 = node2;
            }else {
                node1.next = node2;
            }
            if (count==0){
                node2.next = null;
                node1 = null;
                count = nodes.size();
            }else {
                node1 = node2;
            }
        }
        return root;
    }
}

解法二:递归

注意需要把每个非兄弟节点的next相连,使用while循环,循环中p1指针走向左子树的最右端,p2指针走向右子树的最左端——从而实现非兄弟节点的next相连。

class Solution {
    public Node connect(Node root) {
        if (root == null||root.left==null){
            return root;
        }
        connect(root.left);
        connect(root.right);
        Node p1 = root.left;
        Node p2 = root.right;
        while (p1 != null) {
            p1.next = p2;
            p1 = p1.right;
            p2 = p2.left;
        }
        return root;
    }
}

收获

层序遍历用于解决层内结点关系

  • 17
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值