Diameter of Binary Tree
2017-08-19 18:28
429 查看
题目:
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example:
Given a binary tree
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
Note: The length of path between two nodes is represented by the number of edges between them.
解决:
① 题目要求求出二叉树的直径:二叉树中从一个结点到另一个节点最长的路径,叫做二叉树的直径
我们可以理解为求根节点的左右子树的深度和,那么我们只要对每一个结点求出其左右子树深度之和,就可以更新结果diameter了。为了减少重复计算,我们用hashmap建立每个结点和其深度之间的映射,这样某个结点的深度之前计算过了,就不用再次计算了。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {//321 ms
public int diameterOfBinaryTree(TreeNode root) {
if(root == null) return 0;
int diameter = getDepth(root.left) + getDepth(root.right);
return Math.max(diameter,
Math.max(diameterOfBinaryTree(root.left),diameterOfBinaryTree(root.right)));//不只是头节点
}
public int getDepth(TreeNode node){
Map<TreeNode,Integer> map = new HashMap<>();
if(node == null) return 0;
if(map.containsKey(node) && map.get(node) > 0) return map.get(node);
int depth = Math.max(getDepth(node.left),getDepth(node.right)) + 1;
map.put(node,depth);
return map.get(node);
}
}
② 思路和上一个相同,但是要简洁的多,没有使用map,只使用一个递归即可。在求深度的递归函数中顺便就把直径算出来了。
public class Solution {//10ms
int diameter = 0;
public int diameterOfBinaryTree(TreeNode root) {
getDepth(root);
return diameter;
}
private int getDepth(TreeNode root) {
if(root == null) return 0;
int left = getDepth(root.left);
int right = getDepth(root.right);
diameter = Math.max(diameter, left + right); //不是left + right + 1。。是path。。不是number of node
return Math.max(left, right) + 1;
}
}
③ 进化版。
public class Solution {//9ms
int depth = 0;
public int diameterOfBinaryTree(TreeNode root) {
getDepth(root);
return depth;
}
public int getDepth(TreeNode root) {
if (root == null) return 0;
int left = getDepth(root.left);
int right = getDepth(root.right);
if (depth < left + right) {
depth = left + right;
}
return left > right ? left + 1 : right + 1;
}
}
④ 比较好理解的
class Solution {//23ms
public int diameterOfBinaryTree(TreeNode root) {
if(root == null) return 0;
int diameter = getDepth(root.left) + getDepth(root.right);
return Math.max(diameter,Math.max(diameterOfBinaryTree(root.left),diameterOfBinaryTree(root.right)));
}
public int getDepth(TreeNode root){
if(root == null) return 0;
return Math.max(getDepth(root.left),getDepth(root.right)) + 1;
}
}
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example:
Given a binary tree
1 / \ 2 3 / \ 4 5
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
Note: The length of path between two nodes is represented by the number of edges between them.
解决:
① 题目要求求出二叉树的直径:二叉树中从一个结点到另一个节点最长的路径,叫做二叉树的直径
我们可以理解为求根节点的左右子树的深度和,那么我们只要对每一个结点求出其左右子树深度之和,就可以更新结果diameter了。为了减少重复计算,我们用hashmap建立每个结点和其深度之间的映射,这样某个结点的深度之前计算过了,就不用再次计算了。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {//321 ms
public int diameterOfBinaryTree(TreeNode root) {
if(root == null) return 0;
int diameter = getDepth(root.left) + getDepth(root.right);
return Math.max(diameter,
Math.max(diameterOfBinaryTree(root.left),diameterOfBinaryTree(root.right)));//不只是头节点
}
public int getDepth(TreeNode node){
Map<TreeNode,Integer> map = new HashMap<>();
if(node == null) return 0;
if(map.containsKey(node) && map.get(node) > 0) return map.get(node);
int depth = Math.max(getDepth(node.left),getDepth(node.right)) + 1;
map.put(node,depth);
return map.get(node);
}
}
② 思路和上一个相同,但是要简洁的多,没有使用map,只使用一个递归即可。在求深度的递归函数中顺便就把直径算出来了。
public class Solution {//10ms
int diameter = 0;
public int diameterOfBinaryTree(TreeNode root) {
getDepth(root);
return diameter;
}
private int getDepth(TreeNode root) {
if(root == null) return 0;
int left = getDepth(root.left);
int right = getDepth(root.right);
diameter = Math.max(diameter, left + right); //不是left + right + 1。。是path。。不是number of node
return Math.max(left, right) + 1;
}
}
③ 进化版。
public class Solution {//9ms
int depth = 0;
public int diameterOfBinaryTree(TreeNode root) {
getDepth(root);
return depth;
}
public int getDepth(TreeNode root) {
if (root == null) return 0;
int left = getDepth(root.left);
int right = getDepth(root.right);
if (depth < left + right) {
depth = left + right;
}
return left > right ? left + 1 : right + 1;
}
}
④ 比较好理解的
class Solution {//23ms
public int diameterOfBinaryTree(TreeNode root) {
if(root == null) return 0;
int diameter = getDepth(root.left) + getDepth(root.right);
return Math.max(diameter,Math.max(diameterOfBinaryTree(root.left),diameterOfBinaryTree(root.right)));
}
public int getDepth(TreeNode root){
if(root == null) return 0;
return Math.max(getDepth(root.left),getDepth(root.right)) + 1;
}
}
相关文章推荐
- LeetCode - 543 - Diameter of Binary Tree
- LeetCode.543 Diameter of Binary Tree
- Diameter of Binary tree
- Diameter of Binary Tree
- LeetCode——Diameter of Binary Tree
- leetcode(543). Diameter of Binary Tree
- LeetCode OJ 543 Diameter of Binary Tree [Easy]
- LeetCode 543 Diameter of Binary Tree
- [124]. Binary Tree Maximum Path Sum, [543]. Diameter of Binary Tree
- LeetCode 543: Diameter of Binary Tree
- 【LeetCode】Diameter of Binary Tree 解题报告
- LeetCode 543---- Diameter of Binary Tree
- leetcode-504-Diameter of Binary Tree
- leetcode543 Diameter of Binary Tree
- Data Structure Binary Tree: Diameter of a Binary Tree
- LeetCode Diameter of Binary Tree
- LeetCode@543_Diameter_of_Binary_Tree
- Diameter of Binary Tree问题及解法
- leetcode编程记录9 #543 Diameter of Binary Tree
- Diameter of Binary Tree