Skip to content

Instantly share code, notes, and snippets.

@qiaoxu123
Created December 29, 2018 01:11
Show Gist options
  • Save qiaoxu123/f69a2665b0229086483a51f66150df20 to your computer and use it in GitHub Desktop.
Save qiaoxu123/f69a2665b0229086483a51f66150df20 to your computer and use it in GitHub Desktop.
没有解决,参考solution解决。本质仍然是Binary Tree 的遍历问题
//效率不算高
//Runtime: 8 ms, faster than 55.20%
/**
* 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 {
int ans;
public:
int diameterOfBinaryTree(TreeNode* root) {
ans = 1;
depth(root);
return ans - 1;
}
int depth(TreeNode* node){
if(node == NULL) return 0;
int L = depth(node->left);
int R = depth(node->right);
ans = max(ans,L+R+1);
return max(L,R) + 1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment