- 博客(5)
- 收藏
- 关注
原创 LeetCode 814
递归判断叶子节点是否为0,若叶子节点为0则修剪返回null class Solution { public TreeNode pruneTree(TreeNode root) { if(root == null) return null; help(root); return root; } public TreeNode...
2018-07-22 17:49:04
237
原创 LeetCode 559
给定n-ary树,找到它的最大深度。 最大深度是从根节点到最远叶节点的最长路径上的节点数。 层序遍历。 class Solution { public int maxDepth(Node root) { if(root == null) return 0; Queue<Node> queue = new LinkedList<Nod...
2018-07-22 10:07:48
410
原创 LeetCode 110
给定二叉树,确定它是否是高度平衡的。 从根结点开始递归判断左右子树是否平衡。 class Solution { public boolean isBalanced(TreeNode root) { if(root == null) return true; int left = help(root.left,1); int rig...
2018-07-21 13:30:05
170
原创 Leetcode 543
给定二叉树,计算树的直径长度。 二叉树的直径是树中任意两个节点之间最长路径的长度。 此路径可能会不会通过根节点 class Solution { int res = 0; public int diameterOfBinaryTree(TreeNode root) { if(root == null) return 0; help(root...
2018-07-20 22:40:55
521
原创 LeetCode 404
给出一个二叉树,求出这个二叉树所有左叶子节点的和。 采用DFS递归调用sumOfLeftLeaves,引入一个boolean变量判断是否为左子树。 class Solution { int sum = 0; public int sumOfLeftLeaves(TreeNode root) { if(root == null) return 0; ...
2018-07-20 21:36:53
175
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人