道阻且长,行则将至,走慢一点没关系,不停下就好了.|

Ac_c0mpany丶

园龄:3年7个月粉丝:6关注:3

2023-12-27 18:00阅读: 6评论: 0推荐: 0

[LeetCode Hot 100] LeetCode104. 二叉树的最大深度

题目描述

思路

熟练掌握二叉树的遍历算法

方法一:层序遍历(迭代)+计数

/**
 * 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 int maxDepth(TreeNode root) {
        if (root == null) return 0;
        Deque<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);
        int level = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i ++) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
            level += 1;
        }
        return level;
    }
}

方法二:前序遍历(迭代)

/**
 * 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 int maxDepth(TreeNode root) {
        Deque<Node> stack = new ArrayDeque<>();
        if (root == null) return 0;
        stack.push(new Node(root, 1));
        int res = 0;
        while (!stack.isEmpty()) {
            Node node = stack.pop();
            TreeNode curNode = node.node;
            int depth = node.depth;
            res = Math.max(res, depth);
            if (curNode.right != null) stack.push(new Node(curNode.right, depth + 1)); 
            if (curNode.left != null) stack.push(new Node(curNode.left, depth + 1));   
        }
        return res;
    }
}
class Node {
    TreeNode node;
    int depth;
    Node(TreeNode node, int depth) {
        this.node = node;
        this.depth = depth;
    }
}

方法三:前序遍历(递归)

/**
 * 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 {
    private int res = 0;
    public int maxDepth(TreeNode root) {
        dfs(root, 1);
        return res;
    }
    private void dfs(TreeNode node, int depth) {
        if (node == null) return;
        res = Math.max(res, depth);
        dfs(node.left, depth + 1);
        dfs(node.right, depth + 1);
    }
}

方法四:DFS-自底向上

当前节点的深度 = Max(左子树的最大深度,右子树的最大深度) + 1

/**
 * 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 int maxDepth(TreeNode root) {
        return postOrder(root);
    }
    private int postOrder(TreeNode node) {
        if (node == null) return 0;
        int leftMaxDepth = postOrder(node.left);
        int rightMaxDepth = postOrder(node.right);
        // 处理根节点
        return Math.max(leftMaxDepth, rightMaxDepth) + 1;
    }
}

本文作者:Ac_c0mpany丶

本文链接:https://www.cnblogs.com/keyongkang/p/17931115.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ac_c0mpany丶  阅读(6)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 You Are My Sunshine REOL
You Are My Sunshine - REOL
00:00 / 00:00
An audio error has occurred.

作曲 : Traditional

You are my sunshine

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away

The other night, dear,

When I lay sleeping

I dreamed I held you in my arms.

When I awoke, dear,

I was mistaken

So I hung my head and cried.

You are my sunshine,

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away.

You are my sunshine,

My only sunshine

You make me happy

When skies are gray.

You'll never know, dear

How much I love you

Please don't take my sunshine away

Please don't take my sunshine away.

Please don't take my sunshine away.