Comments (1)
思路
二叉树的遍历方式无非深度优先搜索和广度优先搜索两种
而实现又可分为递归和迭代
递归最简单的了,在这里遍历方式用广度优先搜索
广度优先搜索:
题解
方法一:递归
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if (root == null) {
return 0;
}
let l = maxDepth(root.left);
let r = maxDepth(root.right);
if (root.left == null || root.right == null) {
return l + r + 1;
}
return Math.max(l, r) + 1;
};
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if(root == null) return 0;
return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1);
};
方法二:迭代
var maxDepth = function(root) {
if(root == null) return 0;
let queue = [root];
let level = 0;
while(queue.length) {
let size = queue.length;
while(size --) {
let front = queue.shift();
if(front.left) queue.push(front.left);
if(front.right) queue.push(front.right);
}
// level ++ 后的值代表着现在已经处理完了几层节点
level ++;
}
return level;
};
from leetcode.
Related Issues (20)
- 217.存在重复元素 HOT 1
- 4.寻找两个正序数组的中位数 HOT 1
- 8.字符串转换整数 (atoi) HOT 1
- 237.删除链表中的节点 HOT 1
- 238.除自身以外数组的乘积 HOT 1
- 46.全排列 HOT 1
- 160.相交链表 HOT 1
- 61.旋转链表 HOT 1
- 2.两数相加 HOT 1
- 21.合并两个有序链表 HOT 1
- 92.反转链表 II HOT 1
- 206. 反转链表 HOT 1
- 146. LRU 缓存机制 HOT 1
- 94. 二叉树的中序遍历 HOT 1
- 235. 二叉搜索树的最近公共祖先 HOT 1
- 236. 二叉树的最近公共祖先 HOT 1
- 111. 二叉树的最小深度 HOT 1
- 543. 二叉树的直径 HOT 1
- 124.二叉树中的最大路径和 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode.