The diameter of an N-ary tree is the length of the longest path between any two nodes in the tree. This path may or may not pass through the root. (Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value.) Exam...
开始耍小聪明啦,getMax是求得深度最大值。我们在diameterOfbinaryTree中不断求得左右子树的直径,相加,并且递归,即可获得最大值 /** * Definition for a binary tree node. * struct TreeNode { * int val; * T
[leetcode] 543. Diameter of Binary Tree (easy) 思路: 题目其实就是求左右最长深度的和 classSolution{private:intres =0;public:intdiameterOfBinaryTree(TreeNode *root){dfs(root);returnres; }intdfs(TreeNode *root){if(root ==NULL) {return0; }intleftNum =dfs(root->left);intrightNum =dfs(r...