- 浏览: 137668 次
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
[分析]
[思路1]直接递归时间复杂度是2^h 级别,超时,需要些剪枝技巧:如果从当前节点一直往左的高度 == 一直往右的高度,则以当前节点为根的树是一颗perfect tree,包含的节点数 = 2^h - 1, 其中h 为根节点高度记为1时的树高;否则递归调用count(root.left) + count(root.right) + 1 来计算。剪枝后算法复杂度为h^2,h 为数高,可以这样理解:第一次检查左右子树高度时,高度为h,计算量时O(h),若有第二次,待检查的树高减一,计算量为O(h - 1)……如此递推,因此平均时间复杂度是O(h^2)。
[思路2] 二分枚举。高度为 h 的完全二叉树,其节点数=高度为 h - 1的满二叉树 + 最后一层节点数。如何获取最后一层节点数呢?h 从0开始计,则最后一层满时节点数=2^h。给最后一层几点编号,若记往左走为0,往右走为1,则最后一层节点编号二进制形式正好对应从根节点开始的路径。二分枚举节点并判断其是否存在,直到找到最后一层的最右边节点。
搬一个参考博客上的图:
Lv0 1
/ \
Lv1 2 3
/ \ / \
Lv2 4 5 6 -
No. 00 01 10
[ref]
http://bookshadow.com/weblog/2015/06/06/leetcode-count-complete-tree-nodes/
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
[分析]
[思路1]直接递归时间复杂度是2^h 级别,超时,需要些剪枝技巧:如果从当前节点一直往左的高度 == 一直往右的高度,则以当前节点为根的树是一颗perfect tree,包含的节点数 = 2^h - 1, 其中h 为根节点高度记为1时的树高;否则递归调用count(root.left) + count(root.right) + 1 来计算。剪枝后算法复杂度为h^2,h 为数高,可以这样理解:第一次检查左右子树高度时,高度为h,计算量时O(h),若有第二次,待检查的树高减一,计算量为O(h - 1)……如此递推,因此平均时间复杂度是O(h^2)。
[思路2] 二分枚举。高度为 h 的完全二叉树,其节点数=高度为 h - 1的满二叉树 + 最后一层节点数。如何获取最后一层节点数呢?h 从0开始计,则最后一层满时节点数=2^h。给最后一层几点编号,若记往左走为0,往右走为1,则最后一层节点编号二进制形式正好对应从根节点开始的路径。二分枚举节点并判断其是否存在,直到找到最后一层的最右边节点。
搬一个参考博客上的图:
Lv0 1
/ \
Lv1 2 3
/ \ / \
Lv2 4 5 6 -
No. 00 01 10
[ref]
http://bookshadow.com/weblog/2015/06/06/leetcode-count-complete-tree-nodes/
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { // Method 1: O(h^2) public int countNodes1(TreeNode root) { if (root == null) return 0; if (root.left == null && root.right == null) return 1; TreeNode curr = root; int lHeight = 0; while (curr.left != null) { lHeight++; curr = curr.left; } curr = root; int rHeight = 0; while (curr.right != null) { rHeight++; curr = curr.right; } if (lHeight == rHeight) { return (1 << (lHeight + 1)) - 1; } else { return countNodes(root.left) + countNodes(root.right) + 1; } } // Method 2: Binary Search public int countNodes(TreeNode root) { if (root == null) return 0; int height = 0; TreeNode curr = root; while (curr.left != null) { height++; curr = curr.left; } int left = 0, right = (1 << height) - 1; while (left <= right) { int mid = left + ((right - left) >> 1); if (existsNode(root, mid, height - 1)) { left = mid + 1; } else { right = mid - 1; } } return (1 << height) + right; } public boolean existsNode(TreeNode root, int path, int height) { while (height >= 0 && root != null) { if (((path >> height) & 1) == 1) { root = root.right; } else { root = root.left; } height--; } return root != null; } }
发表评论
-
Smallest Rectangle Enclosing Black Pixels
2016-02-13 12:39 901An image is represented by a bi ... -
Inorder Successor in BST
2015-09-26 17:49 721[分析] 参考https://leetcode.com/dis ... -
Lowest Common Ancestor of A Binary Tree
2015-09-26 11:39 573[分析] 最近公共祖先(LCA)是一个经典问题,以前没有好好 ... -
Leetcode - Closest Binary Search Tree Value II
2015-09-13 14:16 790[分析] 思路1:遍历所有节点找到和 target最接近的 k ... -
Leetcode - H-Index II
2015-09-06 09:39 1101Follow up for H-Index: What if ... -
Leetcode - Graph Valid Tree
2015-09-02 08:50 2274Given n nodes labeled from 0 to ... -
Leetcode - Closest Binary Search Tree Value II
2015-09-01 09:50 3186Given a non-empty binary search ... -
Leetcode - Binary Tree Upside Down
2015-08-29 18:50 416Given a binary tree where all t ... -
Leetcode - Verify Preorder Sequence in Binary Search Tree
2015-08-29 16:46 3032[分析] 思路1:暴力法,遍历当前待检查数组,找到第一个大于数 ... -
Leetcode - Converted Sorted List to BST
2015-08-23 20:07 363Given a singly linked list wher ... -
Leetcode - Insert Interval
2015-08-14 10:12 629[分析] 这道题思路直接,但bug free需要费些心力。第一 ... -
Leetcode - Pow(x, n)
2015-08-11 09:45 465[分析] 数值计算类型题目,二分法或者借助位运算,本题两种方法 ... -
Leetcode - sqrt(x)
2015-08-10 21:40 809[分析] 这是一道数值计算的题目,Code Ganker中指出 ... -
Leetcode - Kth Smallest Element in BST
2015-08-11 10:26 658[分析] 思路1: 递归中序遍历,返回中序遍历中的第k个数。 ... -
Leetcode - Search for a Range
2015-08-10 08:32 500[分析] 思路1: 伪二分查找。若中间元素正好是target时 ... -
二分查找算法小结
2015-08-10 08:32 956在做leetcode二分查找类 ... -
Leetcode - Search in Rotated Sorted Array II
2015-08-09 18:47 481[分析] 同Search in Rotated Sorted ... -
Leetcode - Search in Rotated Sorted Array
2015-08-09 17:47 532[分析] 这题可以看做是Find Minimum in Rot ... -
Leetcode - Find Peak Element
2015-08-09 16:56 606[分析] 暴力法,按序扫描数组,找到peak element。 ... -
Leetcode - Find Minimum in Rotated Sorted Array II
2015-08-09 12:19 814[分析] Find Minimum in Rotated So ...
相关推荐
java java_leetcode题解之Count Complete Tree Nodes.java
java java_leetcode-101-symmetric-tree
java java_leetcode-100-same-tree
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
java java_leetcode-110-balanced-binary-tree
java java_leetcode-maximum-depth-of-binary-tree
java java_leetcode-99-recover-binary-search-tree
"leetcode-tag-Tree" 指的是 LeetCode 上与“树”相关的标签,这通常意味着这些题目涉及到数据结构中的树型结构。树是一种非线性的数据结构,由若干个节点(或称为顶点)和连接这些节点的边组成,每个节点都可能有零...
java java_leetcode-111-minimum-depth-of-binary-tree
java java_leetcode-114-flatten-binary-tree-to-linked-list
java java_leetcode-105-construct-binary-tree-from-preorder-and-inorde
《在IDE中高效进行LeetCode练习:leetcode-editor的深度解析》 在编程学习与技能提升的过程中,LeetCode作为一款广受欢迎的在线编程挑战平台,帮助众多开发者锻炼算法思维,提高编程能力。而为了进一步提升练习体验...
leetcode-习题集资源leetcode-习题集资源leetcode-习题集资源leetcode-习题集资源leetcode-习题集资源leetcode-习题集资源
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
在IDE中解决LeetCode问题,支持leetcode.com与leetcode-cn.com,满足基本的做题需求。 理论上支持: IntelliJ IDEA PhpStorm WebStorm PyCharm RubyMine AppCode CLion GoLand DataGrip Rider MPS Android Studio。
LeetCode Editor 7.4 版本的下载是一个名为 "leetcode-editor" 的压缩包文件。这个压缩包的导入过程非常简单,只需要将它直接拖入 IDEA 界面,IDEA 会自动识别并安装插件。这种方式使得安装过程无需额外的步骤,对于...
leetcode-习题集资源源代码leetcode-习题集资源源代码leetcode-习题集资源源代码leetcode-习题集资源源代码leetcode-习题集资源源代码
~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/vsc-leetcode-cli/bin/leetcode /usr/local/bin/leetcode 修改模板 open ~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/...