leetcode543
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * public int val;
- * public TreeNode left;
- * public TreeNode right;
- * public TreeNode(int x) { val = x; }
- * }
- */
- public class Solution
- {
- Stack<TreeNode> S = new Stack<TreeNode>();//记录访问过的节点
- Dictionary<TreeNode, List<TreeNode>> dic = new Dictionary<TreeNode, List<TreeNode>>();
- private void postTree(TreeNode node)
- {
- if (node != null)
- {
- S.Push(node);
- if (node.left != null)
- {
- postTree(node.left);
- }
- if (node.right != null)
- {
- postTree(node.right);
- }
- if (node.left == null && node.right == null)
- {
- var nodelist = S.ToList();
- nodelist.Reverse();
- if (!dic.ContainsKey(node))
- {
- dic.Add(node, nodelist);
- }
- }
- S.Pop();
- }
- }
- public int DiameterOfBinaryTree(TreeNode root)
- {
- postTree(root);
- var maxDiameter = ;
- foreach (var d in dic)
- {
- foreach (var d2 in dic)
- {
- if (d.Key == d2.Key)
- {
- var diameter = d2.Value.Count - ;
- maxDiameter = Math.Max(maxDiameter, diameter);
- }
- else
- {
- //第一种情况:计算叶子节点到根节点的直径
- var depth = d2.Value.Count - ;
- var c1 = d.Value.Count;
- var c2 = d2.Value.Count;
- var diameter = ;
- if (c1 < c2)
- {
- for (int i = ; i < c1; i++)
- {
- if (d.Value[i] != d2.Value[i])
- {
- diameter = c1 - i + c2 - i;
- break;
- }
- }
- }
- else
- {
- for (int i = ; i < c2; i++)
- {
- if (d.Value[i] != d2.Value[i])
- {
- diameter = c1 - i + c2 - i;
- break;
- }
- }
- }
- //var IncCount = d.Value.Intersect<TreeNode>(d2.Value).Count();
- //var diameter = (c1 - IncCount) + (c2 - IncCount);
- diameter = Math.Max(diameter, depth);
- maxDiameter = Math.Max(maxDiameter, diameter);
- }
- }
- }
- return maxDiameter;
- }
- }
https://leetcode.com/problems/diameter-of-binary-tree/#/description
补充一种递归的方式,使用python实现:
思路是每次递归判断,以当前节点为根节点是否能达到最大,见第14行。
而每次递归向上返回的是当前节点的左右子树的高度的更大的一个,加上当前节点(+1)的高度,见第15行。
- class Solution:
- def __init__(self):
- self.maxnum =
- #隐含的前提,最大的距离应该是两个叶子节点的距离
- #本身这个函数是返回,当前节点到其所有叶子节点的最大深度
- def getLength(self,root):
- if root != None:
- #左子树的最大深度
- left = self.getLength(root.left)
- #右子树的最大深度
- right = self.getLength(root.right)
- #在返回之前,更新maxnum
- self.maxnum = max(self.maxnum,left+right)
- return + max(left,right)
- return
- def diameterOfBinaryTree(self, root: 'TreeNode') -> 'int':
- self.getLength(root)
- return self.maxnum
这道题目与leetcode104 二叉树的最大深度,思想是一样的,可以说是104题的升级版。
leetcode543的更多相关文章
- [Swift]LeetCode543. 二叉树的直径 | Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- LeetCode543. Diameter of Binary Tree
Description Given a binary tree, you need to compute the length of the diameter of the tree. The dia ...
- Leetcode543.Diameter of Binary Tree二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, 它 ...
- LeetCode543.二叉树的直径
题目 1 class Solution { 2 public: 3 int minimum = INT_MIN; 4 vector<int>res; 5 int diameterOfBin ...
- LeetCode树专题
LeetCode树专题 98. 验证二叉搜索树 二叉搜索树,每个结点的值都有一个范围 /** * Definition for a binary tree node. * struct TreeNod ...
随机推荐
- L332 NBA: Dwyane Wade and Dirk Nowitzki Say Emotional Goodbyes
Two games in the NBA ended amid emotional scenes on Tuesday as legends at separate teams marked thei ...
- 大大维的游戏机计划1--贪吃蛇v1
本文为大大维原创,最早于博客园发表,转载请注明出处!!! 虽然本人一直是个免费的游戏测试员(/手动滑稽),但一直有着一个游戏架构师的梦想.正如马爸爸所说,梦想还是要有的,万一实现了呢? 这些天放寒假, ...
- 51单片机数据类型int,float,指针所占字节数
1.int===2个字节 2.sfr===特殊功能寄存器,也是一种扩充数据类型,占用1个内存单元,利用它可以访问51单片机内的所有特殊功能寄存器. sfr P1 = 0x90;/////////这一句 ...
- getfacl
ACL即Access Control List 主要的目的是提供传统的owner,group,others的read,write,execute权限之外的具体权限设置,ACL可以针对单一用户.单一文件 ...
- SignalTap导致PCIe Read/Write卡死
/********************************************************************** * SignalTap导致PCIe Read/Write ...
- ubuntu 16 阿里云 vsftpd
1.安装 apt-get install vsftpd 2.配置 conf vim /etc/vsftpd.conf --禁止匿名用户sed -i "s/anonymous_enable=Y ...
- LeetCode - Unique Email Addresses
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
- 第三章 jQuery总结 参考文本
jQuery jQuery是javascript的一个函数库,非常方便,非常主流 利用jQuery开发步骤: 1导入jQuery库 2在$(function(){})的{}中编写jQuery代码 ①j ...
- xgboost实例代码
# -*- coding: utf-8 -*- import xgboost as xgb import csv import jieba jieba.load_userdict('wordDict. ...
- 【java】接口
class :用于定义类interface:用于定于接口 接口定义时,特点:1.接口中常见定义:常亮和抽象方法2.接口中的成员都有固定修饰符(如果没有会被隐式添加) 常量:public static ...