二叉树练习

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;
    }
}
相关推荐
崎岖Qiu11 天前
leetcode100.相同的树(递归练习题)
算法·leetcode·二叉树·力扣·递归
码破苍穹ovo20 天前
堆----3.数据流的中位数
java·数据结构·算法·力扣
崎岖Qiu20 天前
leetcode1343:大小为K的子数组(定长滑动窗口)
java·算法·leetcode·力扣·滑动窗口
崎岖Qiu22 天前
leetcode643:子数组最大平均数 I(滑动窗口入门之定长滑动窗口)
java·算法·leetcode·力扣·双指针·滑动窗口
老马啸西风1 个月前
java 位运算转换 bit operator convert
java·开发语言·算法·leetcode·面试·力扣·位运算
青小莫1 个月前
c语言-数据结构-二叉树OJ
c语言·开发语言·数据结构·二叉树·力扣
好易学·数据结构1 个月前
可视化图解算法56:岛屿数量
数据结构·算法·leetcode·力扣·回溯·牛客网
Ylinnnnn1 个月前
二分查找法
c++·学习·算法·leetcode·力扣·c·入门
好易学·数据结构2 个月前
可视化图解算法52:数据流中的中位数
数据结构·算法·leetcode·面试·力扣·笔试·牛客
码破苍穹ovo2 个月前
回溯----5.括号生成
java·数据结构·力扣·递归