zoukankan      html  css  js  c++  java
  • 二叉树的最大深度

    104 二叉树的最大深度

    书上的递归法解决(C++)

    /**
     * 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 maxDepth(TreeNode* root) {
            TreeNode* cur = root;
            if(cur == NULL){
                return 0;
            }
            else{
                int i = maxDepth(cur->left);
                int j = maxDepth(cur->right);
                return (i<j)?j+1:i+1;
            }
        }
    };

    由这个题我首先想到的是前几天的层序遍历,把每一层的放在一个链表里面,这个题计算深度就直接统计层数就可以了。

    java代码

    /**
     * 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> que = new LinkedList<>();
            que.offer(root);
            int count = 0,level = 0;
            TreeNode cur = null;
            while(!que.isEmpty()){
                count = que.size();
                level++;
                while(count>=1){
                    cur = que.poll();
                    if(cur.left != null){
                        que.offer(cur.left);
                    }
                    if(cur.right != null){
                        que.offer(cur.right);
                    }
                    count--;
                }
            }
            return level;
        }
    }

    C++代码:

    /**
     * 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 maxDepth(TreeNode* root) {
            if(root==NULL){//首先判断根节点是否为空
                return 0;
            }
            int level = 0;
            queue<TreeNode*> q;
            q.push(root);
            while(!q.empty()){
                int count = q.size();
                level++;
                while(count > 0){
                    TreeNode* cur = q.front();
                    
                    q.pop();
                    
                    if(cur->left != NULL){
                        q.push(cur->left);
                    }
                    if(cur->right != NULL){
                        q.push(cur->right);
                    }
                    count--;
                }
            }
            return level;
        }
    };
  • 相关阅读:
    攻防一体 暴力攻击
    新的亮眼的但不彻底的交互
    利用物联网或智能化区分产品
    Character Sets, Collation, Unicode :: utf8_unicode_ci vs utf8_general_ci
    容灾 RPO RTO
    微信找人代付 下单账号 支付账号
    微信公众号 openId 支付 php中file_get_contents与curl性能比较分析
    t
    accesstoken 中控服务器 并发刷新 加并发锁
    a
  • 原文地址:https://www.cnblogs.com/dong973711/p/10865239.html
Copyright © 2011-2022 走看看