543. Diameter of Binary Tree
来源:互联网 发布:唱歌测音软件 编辑:程序博客网 时间:2024/05/16 13:04
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.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int depth(struct TreeNode * root) { int l = 0; int r = 0; if(root == NULL){ return 0; } l = 1 + depth(root->left); r = 1 + depth(root->right); return l>r?l:r;}int maxOfThree(int a,int b,int c){ int d = a>b?a:b; return d>c?d:c;}int diameterOfBinaryTree(struct TreeNode* root) { if(root == NULL){ return 0; } return maxOfThree(diameterOfBinaryTree(root->left), diameterOfBinaryTree(root->right), depth(root->left)+depth(root->right));}
阅读全文
0 0
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- Diameter of Binary Tree
- Hive参数配置调优
- Python返回数组(List)长度的方法
- 回来了,未来方向PHP && GO,向大厂奋斗
- 问题是什么?
- Intellij IDEA 快捷键整理(NoCare)
- 543. Diameter of Binary Tree
- vs2013 调试libevent 源码
- 观察者模式
- HDU
- Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises)
- 一个设置问题导致,CFileDialog对话框打开不了
- Unity3D UGUI 血条跟随角色——几种方案对比
- 线段树开新坑:kuangbin带你飞
- 基于微软案例数据挖掘之Microsoft 神经网络分析算法原理篇