二叉树练习

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;
    }
}
相关推荐
I AM_SUN7 天前
131. 分割回文串-两种回溯思路
c++·算法·leetcode·深度优先·力扣
鸡鸭扣14 天前
leetcode hot100:十四、解题思路大全:真·大全!
数据结构·python·算法·leetcode·力扣·笔试
白白糖15 天前
相同,对称,平衡,右视图(二叉树)
python·算法·二叉树·力扣
I AM_SUN15 天前
146.LRU缓存-图解LRU
数据结构·c++·算法·leetcode·缓存·力扣
鸡鸭扣19 天前
leetcode hot100:解题思路大全
数据结构·python·算法·leetcode·力扣
Kidddddult25 天前
力扣刷题Day 48:盛最多水的容器(283)
算法·leetcode·力扣
Kidddddult1 个月前
力扣刷题Day 46:搜索二维矩阵 II(240)
算法·leetcode·力扣
zxctsclrjjjcph1 个月前
【递归、搜索和回溯】递归、搜索和回溯介绍及递归类算法例题
开发语言·c++·算法·力扣
好易学·数据结构1 个月前
可视化图解算法36: 序列化二叉树-I(二叉树序列化与反序列化)
数据结构·算法·leetcode·二叉树·力扣·序列化·牛客
Kidddddult1 个月前
力扣刷题Day 43:矩阵置零(73)
算法·leetcode·力扣