当前位置 : 主页 > 手机开发 > harmonyos >

leetcode-543-Diameter of Binary Tree

来源:互联网 收集:自由互联 发布时间:2023-08-26
int dfs(root): the max. length of one of substree. i.e. dfs(root):if(root == null)return 0left += dfs(root-left)right += dfs(root-right)res = max(res, left + right + 1);return max(left, right) + 1; Error: calculate the path not the node, so


int dfs(root): the max. length of one of substree.

i.e. 
		dfs(root):
			if(root == null)
				return 0
			left += dfs(root->left)
			right += dfs(root->right)
			res = max(res, left + right + 1);
			return max(left, right) + 1;

Error:

  1. calculate the path not the node, so we need to -1 at the end.


上一篇:leetcode-208-Implement Trie (Prefix Tree)
下一篇:没有了
网友评论