【问题标题】:Min Depth of binary tree二叉树的最小深度
【发布时间】:2014-08-07 02:31:48
【问题描述】:

我正在阅读二叉树。在练习编码问题时,我遇到了一些解决方案,要求找到二叉树的最小深度。 现在根据我的理解深度是没有从根到节点的边(叶子节点/二叉树的叶子节点)

二叉树{1,2}的最小深度是多少

根据我的解决方案,它应该是 1。

【问题讨论】:

    标签: binary-tree binary-search-tree


    【解决方案1】:

    我的测试解决方案

    public int minDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int ldepth = minDepth(root.left);
        int rdepth = minDepth(root.right);
        if(ldepth == 0){
            return 1+rdepth;
        }else if(rdepth == 0){
            return 1+ldepth;
        }
    
        return (1 + Math.min(rdepth, ldepth));
    }
    

    在这里,我们计算一个节点的 ldepth(最小左子树深度)和 rdepth(最小右子树深度)。然后,如果 ldepth 为零但 rdepth 不为零,则表示当前节点不是叶节点,因此返回 1 + rdepth。如果 rdepth 和 ldepth 都为零,那么当我们为当前叶节点返回 1+0 时,'if' 条件仍然有效。

    'else if' 分支的类似逻辑。在'return'语句中,因为'if'条件都失败了,我们返回1(当前节点)+对左右分支的递归调用的最小值。

    【讨论】:

    • BFS 优于 DFS
    【解决方案2】:

    记住一个叶子节点既没有左子也没有右子。

      1
      /
     /
    2
    

    所以这里 2 是叶节点,但 1 不是。所以这种情况下的最小深度是 2 假设根节点的深度是 1。

    #include<vector>
    #include<iostream>
    #include<climits>
    using namespace std;
    
      struct TreeNode {
          int val;
          TreeNode *left;
          TreeNode *right;
          TreeNode(int x) : val(x), left(NULL), right(NULL) {}
      };
    
    
    class Solution {
    public:
        int minDepth(TreeNode *root) {
    
            if(root == NULL) return 0;
            return getDepth(root);
        }
        int getDepth(TreeNode *r ){
            if(r == NULL) return INT_MAX;
            if(r->left == NULL && r->right == NULL)
                return 1;
            return 1+ min(getDepth(r->left), getDepth(r->right));
        }
    };
    

    【讨论】:

      【解决方案3】:

      根节点的深度为 0,所以这里给定树的深度为 1,请参考下面的递归和迭代解决方案来找到二叉树的最小深度。

      递归解决方案:

      public static int findMinDepth(BTNode root) {
          if (root == null || (root.getLeft() == null && root.getRight() == null)) {
              return 0;
          }
          int ldepth = findMinDepth(root.getLeft());
          int rdepth = findMinDepth(root.getRight());
      
          return (Math.min(rdepth + 1, ldepth + 1));
      }
      

      迭代解决方案:

      public static int minDepth(BTNode root) {
          int minDepth = Integer.MAX_VALUE;
          Stack<BTNode> nodes = new Stack<>();
          Stack<BTNode> path = new Stack<>();
          if (root == null) {
              return -1;
          }
          nodes.push(root);
          while (!nodes.empty()) {
              BTNode node = nodes.peek();
              if (!path.empty() && node == path.peek()) {
                  if (node.getLeft() == null && node.getRight() == null && path.size() <= minDepth) {
                      minDepth = path.size() - 1;
                  }
                  path.pop();
                  nodes.pop();
              } else {
                  path.push(node);
                  if (node.getRight() != null) {
                      nodes.push(node.getRight());
                  }
                  if (node.getLeft() != null) {
                      nodes.push(node.getLeft());
                  }
              }
          }
          return minDepth;
      }
      

      【讨论】:

      • 您的迭代解决方案错误(引发错误)。为什么要弹出路径和节点?你在哪里存储它们的价值?这里没有进展......
      【解决方案4】:

      二叉树的深度是从根到叶子的最长路径的长度。在我看来,深度应该是 2。

      【讨论】:

      • 我同意你的观点,深度应该是 2。
      【解决方案5】:
      public int minDepth(TreeNode root){
              if(root==null)
                  return 0;
              else if(root.left==null && root.right==null)
                  return 1;
              else if(root.left==null)
                  return 1+minDepth(root.right);
              else if(root.right==null) 
                  return 1+minDepth(root.left);
              else
              return 1+Math.min(minDepth(root.right), minDepth(root.left));  
         }
      

      【讨论】:

        【解决方案6】:

        正如其他人所说,解决方案应该是 2...但它是语义的,如果您对深度的定义不同,您可以简单地获取结果并减去 1。

        这是一个迭代答案(在 C# 中)(Rajesh Surana 的答案是一个很好的 Recusive 答案):

        public static int FindMinDepth<T>(BinarySearchTree<T> tree) where T : IComparable<T>
        {
            var current = tree._root;
        
            int level = 0;
            Queue<BSTNode<T>> q = new Queue<BSTNode<T>>();
        
            if (current != null)
                q.Enqueue(current);
        
            while (q.Count > 0)
            {
                level++;
        
                Queue<BSTNode<T>> nq = new Queue<BSTNode<T>>();
                foreach (var element in q)
                {
                    if (element.Left == null && element.Right == null)
                        return level;
        
                    if (element.Left != null) nq.Enqueue(element.Left);
                    if (element.Right != null) nq.Enqueue(element.Right);
                }
        
                q = nq;                
            }
        
            return 0;
            //throw new Exception("Min Depth not found!");
        }
        

        【讨论】:

          【解决方案7】:

          JavaScript 解决方案

          给定以下二叉树结构:

          function BT(value, left = null, right = null) {
            this.value = value;
            this.left = left;
            this.right = right;
          }
          

          找到最小深度的方法可以是这样的:

          BT.prototype.getMinDepth = function() {
            if (!this.value) {
              return 0;
            }
            if (!this.left && !this.right) {
              return 1;
            }
            if (this.left && this.right) {
              return Math.min(this.left.getMinDepth(), this.right.getMinDepth()) + 1;
            }
            if (this.left) {
              return this.left.getMinDepth() + 1;
            }
            if (this.right) {
              return this.right.getMinDepth() + 1;
            }
          }
          

          上述解决方案的时间复杂度为 O(n),因为它遍历所有树节点。

          更好的运行时解决方案将是使用在到达第一个叶节点时结束的广度遍历方法:

          BT.prototype.getMinDepth = function(depth = 0) {
            if (!this.value) {
              return depth;
            }
          
            depth++;
          
            if (!this.left || !this.right) {
              return depth;
            }
          
            return Math.min(this.left.getMinDepth(depth), this.right.getMinDepth(depth));
          }
          

          【讨论】:

            【解决方案8】:

            给定路径的深度是沿该路径从根到叶节点的节点数。最小值是从根到 LEAF 节点的节点数最少的路径。在这种情况下,唯一的叶子节点是2。(叶子节点被定义为没有孩子的节点)因此,唯一的深度和最小深度都是2。

            Java 示例代码:

            public class Solution {
                public int minDepth(TreeNode root) {
                    if (root==null) return 0;
                    if ((root.left==null) || (root.right==null)) {
                        return 1+Math.max(minDepth(root.left),minDepth(root.right));
                    }
                    return 1+Math.min(minDepth(root.left),minDepth(root.right));
                }
            }
            

            【讨论】:

              【解决方案9】:

              最小深度是左子树和右子树深度中的最小值。

              public static int maxDepth(TreeNode root) {
                  if(root == null) {
                      return 0;
                  }
              
                  return getDepth(root);
              }
              
              private static int getDepth(TreeNode a) {
                  if(a.left == null & a.right == null) {
                      return 1;
                  }
                  int leftDepth = 0;
                  int rightDepth = 0;
                  if(a.left != null) {
                      leftDepth = getDepth(a.left);
                  }
                  if(a.right != null) {
                      rightDepth = getDepth(a.right);
                  }
                  return (Math.min(leftDepth, rightDepth)+1);
              }
              

              【讨论】:

                【解决方案10】:

                二叉树的最小深度是指从根到叶节点的最短距离。尽管二叉树的 minDepth 是 1 还是 2 是有争议的,这取决于您是否想要到空节点的最短距离,在这种情况下,答案将是 1 或到同级为 ALSO 的空节点的最短距离空节点,在这种情况下,二叉树{1,2}的答案是2。一般是前者,按照Cracking the Coding Interview中提到的算法,我们有解决方案

                int minDepth(TreeNode root) {
                    if (root == null) { return 0;}
                        return 1 + Math.min(minDepth(root.left), minDepth(root.right));
                }
                

                【讨论】:

                • 本方案未考虑以下情况。
                • 其中一个孩子为空时不考虑
                猜你喜欢
                • 2021-07-30
                • 2016-01-24
                • 2021-09-19
                • 2015-02-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多