LeetCode 104. Maximum Depth of Binary Tree

xiaoxiao2021-02-28  29

public int maxDepth(TreeNode root) { return order(root, 0); } public int order(TreeNode node, int depth) { if(node == null) { return depth; } depth++; int d1 = order(node.left, depth); int d2 = order(node.right, depth); return Math.max(d1, d2); }

深度优先搜索,把左右子树最大深度往上传。

转载请注明原文地址: https://www.6miu.com/read-2619692.html

最新回复(0)