Created
December 29, 2018 01:11
-
-
Save qiaoxu123/f69a2665b0229086483a51f66150df20 to your computer and use it in GitHub Desktop.
没有解决,参考solution解决。本质仍然是Binary Tree 的遍历问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//效率不算高 | |
//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; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment