二叉树练习

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;
    }
}
相关推荐
满怀10158 小时前
【LeetCode】1.两数之和
力扣
Y1nhl7 天前
力扣hot100_技巧_python版本
开发语言·python·算法·力扣
Y1nhl9 天前
基础算法:滑动窗口_python版本
开发语言·python·算法·力扣·滑动窗口
白白糖13 天前
组合与括号生成(回溯)
python·算法·力扣
GGBondlctrl16 天前
【leetcode】记录与查找:哈希表的题型分析
算法·力扣·两数之和·字母异位词分组·存在重复字符2
白白糖17 天前
二叉树 递归
python·算法·力扣
前端 贾公子23 天前
每日一题 == 674. 最长连续递增序列
前端·javascript·力扣
LUCIAZZZ23 天前
基础排序算法
java·数据结构·算法·排序算法·力扣
小辉同志1 个月前
146.LRU缓存
c++·算法·链表·缓存·力扣