二叉树练习

102. 二叉树的层序遍历 - 力扣(LeetCode)

使用队列进行层序遍历。

java 复制代码
/**
 * 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<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>>ans = new ArrayList<>();
        Queue<TreeNode>q = new LinkedList<>();
        if(root == null)
        {
            return ans;
        }
        q.offer(root);
        int num = 1;
        while(!q.isEmpty())
        {
            List<Integer>temp = new ArrayList<>();
            num = q.size();
            while(num != 0)
            {
                TreeNode t = q.poll();
                temp.add(t.val);
                if(t.left != null)
                {
                    q.offer(t.left);
                }
                if(t.right != null)
                {
                    q.offer(t.right);
                }
                num--;
            }
            ans.add(temp);
        }
        return ans;
    }
}

107. 二叉树的层序遍历 II - 力扣(LeetCode)

在上一题的基础上,翻转一下ans

java 复制代码
/**
 * 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<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>>ans = new ArrayList<>();
        Queue<TreeNode>q = new LinkedList<>();
        if(root == null)
        {
            return ans;
        }
        q.offer(root);
        int num = 1;
        while(!q.isEmpty())
        {
            List<Integer>temp = new ArrayList<>();
            num = q.size();
            while(num != 0)
            {
                TreeNode t = q.poll();
                temp.add(t.val);
                if(t.left != null)
                {
                    q.offer(t.left);
                }
                if(t.right != null)
                {
                    q.offer(t.right);
                }
                num--;
            }
            ans.add(temp);
        }
        Collections.reverse(ans);
        return ans;
    }
}

199. 二叉树的右视图 - 力扣(LeetCode)

java 复制代码
/**
 * 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> rightSideView(TreeNode root) {
        List<Integer>ans = new ArrayList<>();
        Queue<TreeNode>q = new LinkedList<>();
        if(root == null)
        {
            return ans;
        }
        q.offer(root);
        int num = 1;
        while(!q.isEmpty())
        {
            num = q.size();
            while((num - 1) != 0)
            {
                TreeNode temp = q.poll();
                if(temp.left != null)
                {
                    q.offer(temp.left);
                }
                if(temp.right != null)
                {
                    q.offer(temp.right);
                }
                num--;
            }
            TreeNode t = q.poll();
            if(t.left != null)
            {
                q.offer(t.left);
            }
            if(t.right != null)
            {
                q.offer(t.right);
            }
            ans.add(t.val);
        }
        return ans;
    }
}

637. 二叉树的层平均值 - 力扣(LeetCode)

java 复制代码
/**
 * 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<Double> averageOfLevels(TreeNode root) {
        List<Double>ans = new ArrayList<>();
        Queue<TreeNode>q = new LinkedList<>();
        if(root == null)
        {
            return ans;
        }
        q.offer(root);
        int num = 1;
        while(!q.isEmpty())
        {
            num = q.size();
            int tnum = num;
            long ad = 0;
            while(num != 0)
            {
                TreeNode temp = q.poll();
                if(temp.left != null)
                {
                    q.offer(temp.left);
                }
                if(temp.right != null)
                {
                    q.offer(temp.right);
                }
                ad += temp.val;
                num--;
            }
            double cur = (double)ad / (double)tnum;
            ans.add(cur);
        }
        return ans;
    }
}

429. N 叉树的层序遍历 - 力扣(LeetCode)

java 复制代码
/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>>ans = new ArrayList<>();
        if(root == null)
        {
            return ans;
        }
        Queue<Node>q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty())
        {
            List<Integer>temp = new ArrayList<>();
            int num = q.size();
            while(num != 0)
            {
                Node t = q.poll();
                temp.add(t.val);
                List<Node>list = t.children;
                for(Node n : list)
                {
                    q.offer(n);
                }
                num--;
            }
            ans.add(temp);
        }
        return ans;
    }
}

515. 在每个树行中找最大值 - 力扣(LeetCode)

java 复制代码
/**
 * 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> largestValues(TreeNode root) {
        List<Integer>ans = new ArrayList<>();
        if(root == null)
        {
            return ans;
        }
        Queue<TreeNode>q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty())
        {
            int num = q.size();
            int max = Integer.MIN_VALUE;
            while(num != 0)
            {
                TreeNode temp = q.poll();
                if(temp.left != null)
                {
                    q.offer(temp.left);
                }
                if(temp.right != null)
                {
                    q.offer(temp.right);
                }
                max = max > temp.val ? max : temp.val;
                num--;
            }
            ans.add(max);
        }
        return ans;
    }
}
相关推荐
崎岖Qiu5 天前
leetcode380:RandomizedSet - O(1)时间插入删除和获取随机元素(数组+哈希表的巧妙结合)
java·数据结构·算法·leetcode·力扣·散列表
好易学·数据结构5 天前
可视化图解算法60: 矩阵最长递增路径
数据结构·算法·leetcode·力扣·递归·回溯算法·牛客
ShineWinsu10 天前
对于单链表相关经典算法题:206. 反转链表及876. 链表的中间结点的解析
java·c语言·数据结构·学习·算法·链表·力扣
闻缺陷则喜何志丹16 天前
【有序集合 有序映射 懒删除堆】 3510. 移除最小数对使数组有序 II|2608
c++·算法·力扣·有序集合·有序映射·懒删除堆
崎岖Qiu1 个月前
leetcode100.相同的树(递归练习题)
算法·leetcode·二叉树·力扣·递归
码破苍穹ovo1 个月前
堆----3.数据流的中位数
java·数据结构·算法·力扣
崎岖Qiu1 个月前
leetcode1343:大小为K的子数组(定长滑动窗口)
java·算法·leetcode·力扣·滑动窗口
崎岖Qiu1 个月前
leetcode643:子数组最大平均数 I(滑动窗口入门之定长滑动窗口)
java·算法·leetcode·力扣·双指针·滑动窗口
老马啸西风2 个月前
java 位运算转换 bit operator convert
java·开发语言·算法·leetcode·面试·力扣·位运算
青小莫2 个月前
c语言-数据结构-二叉树OJ
c语言·开发语言·数据结构·二叉树·力扣