Skip to content

Commit 9769527

Browse files
committed
完全二叉树节点和(引入满二叉树概念, 区分求解)
1 parent a4bbaef commit 9769527

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

ProblemsList.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
* 144\. [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/description)
142142
* 145\. [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/description)
143143
* 173\. [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/description)
144+
* 222\. [Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/description)
144145
* 226\. [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/description)
145146
* 235\. [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description)
146147
* 236\. [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description)
@@ -164,7 +165,6 @@
164165
* 153\. [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description)
165166
* 154\. [Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description)
166167
* 162\. [Find Peak Element](https://leetcode.com/problems/find-peak-element/description)
167-
* 222\. [Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/description)
168168
* 230\. [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description)
169169
* 275\. [H-Index II](https://leetcode.com/problems/h-index-ii/description)
170170
* 278\. [First Bad Version](https://leetcode.com/problems/first-bad-version/description)

Tree/222_CountCompleteTreeNodes.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# coding: utf8
2+
3+
4+
"""
5+
题目链接: https://leetcode.com/problems/count-complete-tree-nodes/description.
6+
题目描述:
7+
8+
Given a complete binary tree, count the number of nodes.
9+
10+
Definition of a complete binary tree from Wikipedia:
11+
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last
12+
level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
13+
14+
"""
15+
16+
17+
# Definition for a binary tree node.
18+
class TreeNode(object):
19+
def __init__(self, x):
20+
self.val = x
21+
self.left = None
22+
self.right = None
23+
24+
25+
class Solution(object):
26+
def countNodes(self, root):
27+
"""
28+
:type root: TreeNode
29+
:rtype: int
30+
"""
31+
if not root:
32+
return 0
33+
34+
# 满二叉树, 直接返回
35+
# 不然, leetcode大数据样例通不过测试
36+
lh = self.left_tree_height(root)
37+
rh = self.right_tree_height(root)
38+
if lh == rh:
39+
return pow(2, lh) - 1
40+
41+
return 1 + self.countNodes(root.left) + self.countNodes(root.right)
42+
43+
def left_tree_height(self, root):
44+
if not root:
45+
return 0
46+
47+
return 1 + self.left_tree_height(root.left)
48+
49+
def right_tree_height(self, root):
50+
if not root:
51+
return 0
52+
53+
return 1 + self.right_tree_height(root.right)

0 commit comments

Comments
 (0)