03.求给定二叉树的最大深度, 最大深度是指树的根结点到最远叶子结点的最长路径上结点的数量。

解法1:递归

public static int maxDepth(TreeNode root) {
        // write code here
        if (root == null) {
            return 0;
        }
        return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
    }

解法二:队列

public static class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(3);
        TreeNode treeNode2 = new TreeNode(9);
        TreeNode treeNode3 = new TreeNode(20);
        TreeNode treeNode4 = new TreeNode(15);
        TreeNode treeNode5 = new TreeNode(7);
        treeNode.left = treeNode2;
        treeNode.right = treeNode3;
        treeNode2.left = treeNode4;
        treeNode2.right = treeNode5;
        System.out.println(maxDepth(treeNode));
    }

    /*
     * 使用队列来是实现层序遍历
     */
    public static int maxDepth(TreeNode root) {
        // write code here
        if (root == null) {
            return 0;
        }
        int count = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int elementSize = queue.size();
            for (int i = 0; i < elementSize; i++) {
                if (queue.element().left != null) {
                    queue.offer(queue.element().left);
                }
                if (queue.element().right != null) {
                    queue.offer(queue.element().right);
                }
                queue.poll();
            }
            count++;
        }
        return count;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我们可以使用深度优先搜索(DFS)来解决这个问题。从节点开始,对于每个叶子节点,记录从节点到该叶子节点的路径,并比较该路径的长度和最长路径的长度。如果该路径的长度大于最长路径的长度,则更新最长路径。 具体实现步骤如下: 1. 定义一个全局变量 maxLen,表示最长路径的长度,一个全局变量 maxPath,表示最长路径。 2. 定义一个 DFS 函数,接受当前节点 node 和当前路径 path 作为参数。 3. 如果 node 是叶子节点,计算 path 的长度,并与 maxLen 比较,如果 path 的长度大于 maxLen,则更新 maxLen 和 maxPath。 4. 如果 node 不是叶子节点,递归遍历 node 的左子树和右子树,分别传入更新后的 path。 5. 在主函数中调用 DFS(root, []),其中 root 是二叉树节点,[] 表示初始路径为空。 6. 输出 maxPath。 代码如下: ```python class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None maxLen = 0 maxPath = [] def DFS(node, path): global maxLen, maxPath if not node.left and not node.right: # node is leaf node path.append(node.val) if len(path) > maxLen: maxLen = len(path) maxPath = path[:] path.pop() else: path.append(node.val) if node.left: DFS(node.left, path) if node.right: DFS(node.right, path) path.pop() def longest_path(root): global maxLen, maxPath DFS(root, []) return maxPath[::-1] # test root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.left.right = TreeNode(5) root.right.left = TreeNode(6) root.right.right = TreeNode(7) print(longest_path(root)) # [4, 2, 1] ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值