• leetcode-111. Minimum Depth of Binary Tree


    • 平时练习一下leetcode

    • 先根据牛课网上题练习,同步leetcode官网

    题目

    • Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

    思路

    • 本题要注意最小深度与最大深度的区别:对于最大深度,不需要考虑当前子树是否为单子树(即一侧树深度为0)的情况,即最大深度一直等于左右子树的最大值;对于最小深度,需要考虑当前子树是否为单子树的情况,对于双子树,其最小深度为左右子树的最小值,对于单子树,其最小深度为左右深度的最大值(因为一侧的子树为0).

    Nowcoder

    class Solution {
    public:
        int run(TreeNode *root) {  //叶子结点才记录深度,然后不断更新
            if(root==nullptr)
                return 0;
            int l_depth=0,r_depth=0;
           
            l_depth=run(root->left);
            r_depth=run(root->right);
            
            if(l_depth==0)
                return r_depth+1;
            if(r_depth==0)
                return l_depth+1;
            if(l_depth<r_depth)
                return l_depth+1;
            else 
                return r_depth+1;
        }
    };
    

    leetcode

    /**
     * Definition for a binary tree node.
     * 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) return 0;
            if(!root->left) return 1 + minDepth(root->right);
            if(!root->right) return 1 + minDepth(root->left);
            return 1+min(minDepth(root->left),minDepth(root->right));
        }
    };
    
    
  • 相关阅读:
    windows运行shell脚本
    Android Webview H5资源本地化
    Vscode开发Python环境安装
    Vscode开发Java环境搭建
    为什么索引可以提高效率?
    Java内存模型
    栈和队列----向有序的环形单链表中插入新节点
    栈和队列----单链表的选择排序
    栈和队列----将搜索二叉树转换成双向链表
    栈和队列----在单链表中删除指定值的节点
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/6847839.html
Copyright © 2020-2023  润新知