LeetCode刷题Easy篇求二叉树的最大深度

题目

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.

我的尝试

递归尝试

这个很明显可以用递归来解决,代码如下,测试通过:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
       if(root==null){
           return 0;
       }
       return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
    }
}

非递归尝试

我按照之前总结对二叉树处理对方式,写出了代码框架:

1 root入stack

2,while stack is not empty

3. 处理弹出的栈元素,判断是否有left right,入stack。

无法解法这个题目的原因是:

左右节点重复计数?左右节点都入stack后,弹出左节点,加1,弹出右节点也加1,如何取出重复?

看了讨论,发现当时我也感觉到了这个方法,但是没有成熟,其实很简单。循环stack直到size为0,这样同一层的元素都可以出来,此时我们增加1.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
      if(root==null) return 0;
       Deque<TreeNode> stack=new LinkedList();
       stack.push(root);
       int count=0;
       while(!stack.isEmpty()){
           int size=stack.size();
           //这个循环,每层元素的下一层一次性入stack,防止左右子树重复计数
           while(size-->0){
              TreeNode treeNode=stack.pop();
              if(treeNode.left!=null){
                 stack.push(treeNode.left);
               }
              if(treeNode.right!=null){
               stack.push(treeNode.right);
              } 
           }
           count++;
       }
        return count;
    }
}

本来逻辑感觉是正确的,但是测试结果不对,我梳理也没有发现问题,后来看别人的代码,发现区别点,是Deque的api用的不对。

刚开始,stack里面是1,pop出1,然后2和3进入stack。在size循环中,3先pop出stack,所以3的孩子节点将会入stack,这个时候2并没有出stack!!!跟我预期的是不一样的,这就是我错误的地方

怎么解决?应该用队列,而不是栈。这个代码其实是广度有限遍历的算法。所以广度优先遍历利用的是队列,而深度优先遍历用的是栈!

修改后代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
      if(root==null) return 0;
       Queue<TreeNode> queue=new LinkedList();
       queue.add(root);
       int count=0;
       while(!queue.isEmpty()){
           int size=queue.size();
           //这个循环,每层元素的下一层一次性入stack,防止左右子树重复计数
           while(size-->0){
              TreeNode treeNode=queue.poll();
              if(treeNode.left!=null){
                 queue.add(treeNode.left);
               }
              if(treeNode.right!=null){
               queue.add(treeNode.right);
              } 
           }
           count++;
       }
        return count;
    }
}

如果该用Deque,也可以,代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
      if(root==null) return 0;
       Deque<TreeNode> queue=new LinkedList();
       queue.add(root);
       int count=0;
       while(!queue.isEmpty()){
           int size=queue.size();
           //这个循环,每层元素的下一层一次性入stack,防止左右子树重复计数
           while(size-->0){
              TreeNode treeNode=queue.poll();
              if(treeNode.left!=null){
                 queue.add(treeNode.left);
               }
              if(treeNode.right!=null){
               queue.add(treeNode.right);
              } 
           }
           count++;
       }
        return count;
    }
}

猜你喜欢

转载自blog.csdn.net/hanruikai/article/details/84755282