104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. LeetCode104. Maximum Depth of Binary Tree 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最大深度 3. Java 实现 class TreeNode…
今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的.由于c++版的代码网上比較多.所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做LeetCode的题目还是不错的,对以后找工作面试也有帮助! 刚開始就从AC率最高的入手吧! 1.Given an array of integers, every element appears twice except for one. Find that single one. Note: Your…
Maximum Depth of Binary Tree 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. 求二叉树的高度,即从根节点到叶节点上所有节点的数量. 解题思路: 这题是难度系数1,频率1的题目……用到的知识点是二叉树D…
Maximum Depth of Binary Tree  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. 与Minimum Depth of Binary Tree对照看 解法一:递归,子树高度+1. /** * Defini…
LeetCode:Minimum Depth of Binary Tree 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. 算法1:dfs递归的求解 class Solution { public: int minDepth(T…
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = function(num) { var b = (num-1) % 9 + 1 ; return b; }; //之所以num要-1再+1;是因为特殊情况下:当num是9的倍数时,0+9的数字根和0的数字根不同. 性质说明 1.任何数加9的数字根还是它本身.(特殊情况num=0)        小学学加法的…
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * 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…
LeetCode--Maximum Depth of Binary Tree Question 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. Answer /** * Definition for a binary tree…
Minimum Depth of Binary Tree 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. 比较左子树和右子树的深度,取小的那个返回上来,并+1. 需要注意的是如果没有左子树或右子树.那么无条件取存在的那一边子树深…
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. Maximum Depth of Binary Tree Given a binary tree, find i…