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

    https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnd69e/

    Recursion

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        public int maxDepth(TreeNode root) {
            return root == null ? 0: Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
        }
    }
    
    
    
    
    
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
     * };
     */
     #include <algorithm>    // std::max
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            return root == nullptr ? 0 : std::max(maxDepth(root->left),maxDepth(root->right)) + 1;
        }
    };
    
    
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, val=0, left=None, right=None):
    #         self.val = val
    #         self.left = left
    #         self.right = right
    class Solution(object):
        def maxDepth(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            return 0 if root == None else max(self.maxDepth(root.left),self.maxDepth(root.right))+1
    

    BFS

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        public int maxDepth(TreeNode root) {
            if (root == null)
                return 0;
            //创建一个队列
            Deque<TreeNode> deque = new LinkedList<>();
            deque.push(root);
            int count = 0;
            while (!deque.isEmpty()) {
                //每一层的个数
                int size = deque.size();
                while (size-- > 0) {
                    TreeNode cur = deque.pop();
                    if (cur.left != null)
                        deque.addLast(cur.left);
                    if (cur.right != null)
                        deque.addLast(cur.right);
                }
                count++;
            }
            return count;
        }
    
    
    }
    

    DPS

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
         public int maxDepth(TreeNode root) {
            if (root == null)
                return 0;
            //stack记录的是节点,而level中的元素和stack中的元素
            //是同时入栈同时出栈,并且level记录的是节点在第几层
            Stack<TreeNode> stack = new Stack<>();
            Stack<Integer> level = new Stack<>();
            stack.push(root);
            level.push(1);
            int max = 0;
            while (!stack.isEmpty()) {
                //stack中的元素和level中的元素同时出栈
                TreeNode node = stack.pop();
                int temp = level.pop();
                max = Math.max(temp, max);
                if (node.left != null) {
                    //同时入栈
                    stack.push(node.left);
                    level.push(temp + 1);
                }
                if (node.right != null) {
                    //同时入栈
                    stack.push(node.right);
                    level.push(temp + 1);
                }
            }
            return max;
        }
    
    
    
    }
    
  • 相关阅读:
    Qt之课外实践——文件操作(简单清道夫)
    【转载】2018 hosts 持续更新访问 gu歌【更新于:2018-05-03】
    NBU基本常用命令
    运维的四个发展阶段,看看自己在哪个阶段,聊聊怎么升级打怪
    云笔记使用心得分享
    LNMP一键安装包
    expect脚本中,变量的写法
    VERITAS NETBACKUP运维手册(工作总结)
    善用良器:帮你有效管理时间的7种工具
    RAID0 1 5 10原理、种类及性能优缺点对比
  • 原文地址:https://www.cnblogs.com/chenjo/p/14757178.html
Copyright © 2011-2022 走看看