[Tree] C012_N tree maximum depth (dfs & recursive iterations | sequence traversal iteration)

One, Title Description

Given a n-ary 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.

Nary-Tree input serialization is represented in their level order traversal, 
each group of children is separated by the null value (See examples).

Input: root = [1,null,3,2,4,null,5,6]
Output: 3

Second, the problem solution

Method One: recursive

class Solution {
    public int maxDepth(Node root) {
        return dfs(root);
    }
    private int dfs(Node root) {
        if (root == null)
            return 0;
        int max = 0;
        for (Node child : root.children) {
            max = Math.max(dfs(child), max);
        }
        return max + 1;
    }
} 

stack iteration

Here, because of the FILO stack characteristics, we can not remove the first node pushed onto the stack, so we will not order from the top node into the team. So we need to use a curDepth record height of each layer node.

public int maxDepth(Node root) {
  if (root == null)   
      return 0;
  int maxDepth = 0;
  int curDepth = 1;
  Stack<Pair> stack = new Stack<>();
  stack.push(new Pair(root, curDepth));

  while (!stack.isEmpty()) {
      Pair pair = stack.pop();
      curDepth = pair.depth;
      Node node = pair.node;
      maxDepth = Math.max(maxDepth, curDepth);
      
      for (Node child : pair.node.children) {
          stack.push(new Pair(child, curDepth + 1));
      }
  }
  return maxDepth;
}

class Pair {
  Node node;
  int depth;
  public Pair(Node _node, int _depth) {
      node = _node;
      depth = _depth;
  }
}

Complexity Analysis

  • time complexity: O ( n ) O (n) ,
  • Space complexity: O ( n ) O (n) ,

Method Two:

* The wrong version , the size is not one node.children,size(), but queue.size(),

public int maxDepth(Node root) {
  int maxDepth = 0;
  Queue<Node> queue = new LinkedList<>();
  queue.add(root);

  while (!queue.isEmpty()) {
      Node node = queue.poll();
      int level_size = queue.size();
      while (level_size-- > 0) {
          for (Node n : node.children) {
              queue.add(n);
          }
      }
      maxDepth++;
  }
  return maxDepth;
}

* Correct version , just remove every layer, every time the child out of all the nodes of a layer of children into the team.

public int maxDepth(Node root) {
  if (root == null)   
      return 0;
  int maxDepth = 0;
  Queue<Node> queue = new LinkedList<>();
  queue.add(root);

  while (!queue.isEmpty()) {
      int level_size = queue.size();
      while (level_size-- > 0) {
          Node node = queue.poll();
          for (Node n : node.children) {
              queue.add(n);
          }
      }
      maxDepth++;
  }
  return maxDepth;
}

Complexity Analysis

  • time complexity: O ( n ) O (n) ,
  • Space complexity: O ( n ) O (n) ,
Published 495 original articles · won praise 105 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_43539599/article/details/104797685