Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LC 117. 填充每个节点的下一个右侧节点指针II #31

Open
HealUP opened this issue May 18, 2023 · 0 comments
Open

LC 117. 填充每个节点的下一个右侧节点指针II #31

HealUP opened this issue May 18, 2023 · 0 comments
Labels
算法 记录刷题记录,代码随想录

Comments

@HealUP
Copy link
Owner

HealUP commented May 18, 2023

117. 填充每个节点的下一个右侧节点指针II

该题目和116的区别:就是116题是完全二叉树,这道题不是完全二叉树,题解完全一样的。

3.9 104. 二叉树的最大深度

思路:依旧是层序遍历,计算共有多少层即可

class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> que = new LinkedList<>();
        que.offer(root);
        int count = 0;//记录深度
        while (!que.isEmpty()) {
            int len = que.size();
            count++;
            for (int i = 0; i < len; i++) {
                TreeNode node = que.poll();//弹出并记录
                //放入节点的左右节点
                if (node.left != null) que.offer(node.left);
                if (node.right != null) que.offer(node.right);
            }
        }
        return count;
    }
}
@HealUP HealUP added 算法 记录刷题记录,代码随想录 二叉树 labels May 18, 2023
@HealUP HealUP removed the 二叉树 label Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
算法 记录刷题记录,代码随想录
Projects
None yet
Development

No branches or pull requests

1 participant