problem
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its depth = 3.
复制代码
translation:
Given a binary tree, seeking its maximum depth. The maximum depth is the number of nodes on the longest path from the root to the farthest leaf node. Note: The leaves are the nodes have no children. Examples: Given binary tree [3,9,20, null, null, 15,7],
3 / \ 9 20 / \ 15 7 复制代码
The result is 3
Problem-solving ideas
This question is to get a tree of depth, the general design of the tree to the title to or a little trouble, think of the first step count depth? Can hierarchically traversed, I do not know how many layers of the Well. This is one way, but from a different point of view to a tree depth is not determined by its left and right nodes Well, if you add a node has about the same token, in turn, about the depth of their sub-node about node decision, the choice of the depth values, looks like we can solve this problem the
Problem-solving approach
-
According to traverse hierarchical way
public int maxDepth(TreeNode root) { if (root == null) { return 0; } int result = 1; //定义一个队列 List<TreeNode> list = new LinkedList<>(); putNode(root, list); while (list.size() > 0) { //通过遍历的方式把队列里面的数据获取,并把左右子节点塞入 int size = list.size(); while (--size >= 0) { TreeNode treeNode = list.get(size); putNode(treeNode, list); list.remove(size); } result++; } return result; } private void putNode(TreeNode treeNode, List<TreeNode> list) { if (treeNode == null) { return; } if (treeNode.left != null) { list.add(treeNode.left); } if (treeNode.right != null) { list.add(treeNode.right); } } class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } 复制代码
Time Complexity : The scheme used to traverse the hierarchical manner, each corresponding to a time complexity traversal, it is O (n) = O (n )
Space complexity : The program uses additional space, using the temporary array tree, the tree is equivalent to the conversion array, so the space complexity of O (n) = O (n )
-
Recursive divide and conquer:
public int maxDepth(TreeNode root) { if (root == null) { return 0; } return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1); } public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } 复制代码
Time Complexity : The program with the recursive traversal of the embodiment, the time complexity of each of the traversal corresponds to, so to O (n) = O (n )
Space complexity : the program does not use the extra space, so that the space complexity O (n) = O (1 )
to sum up
Solution of this problem is substantially above the appeal, according to the hierarchy traversal in fact not very good effect, remove personal estimate is a result of the operation, if you can not delete, direct the array instead of a tree, the effect may be better.
Reproduced in: https: //juejin.im/post/5cfcad26f265da1b7e102a90