File tree Expand file tree Collapse file tree 13 files changed +348
-101
lines changed
Expand file tree Collapse file tree 13 files changed +348
-101
lines changed Original file line number Diff line number Diff line change 1111| [ LeetCode-哈希表] ( ./leetcode_hash/main.md ) | leetcode哈希表类算法练习|
1212| [ LeetCode-栈+队列] ( ./leetcode_stack/main.md ) | leetcode栈+队列类算法练习|
1313| [ LeetCode-二叉树] ( ./leetcode_tree/main.md ) | leetcode二叉树类算法练习|
14+ | [ LeetCode-其他] ( ../leetcode_others/main.md ) | leetcode其他分类算法练习|
1415| [ Django学习] ( ./django_note/main.md ) | Django学习笔记。|
1516| [ 其他] ( ./others/main.md ) | redis,kafka,ZeroMQ等|
Original file line number Diff line number Diff line change 4444* [ 二叉树的层序遍历] ( 二叉树的层序遍历.md )
4545* [ 反转二叉树] ( 反转二叉树.md )
4646* [ 对称二叉树] ( 对称二叉树.md )
47+ * [ 相同的树] ( 相同的树.md )
48+ * [ 另一个树的子树] ( 另一个树的子树.md )
4749* [ 二叉树的最大深度] ( 二叉树的最大深度.md )
4850* [ 二叉树的最小深度] ( 二叉树的最小深度.md )
4951* [ 完全二叉树的节点个数] ( 完全二叉树的节点个数.md )
52+ * [ 平衡二叉树] ( 平衡二叉树.md )
53+ * [ 二叉树的所有路径] ( 二叉树的所有路径.md )
54+ * [ 左叶子之和] ( 左叶子之和.md )
55+ * [ 找树左下角的值] ( 找树左下角的值.md )
56+ * [ 路径总和] ( 路径总和.md )
5057
51- 1 . [ 相同的树] ( 相同的树.md )
52-
53- 2 . [ 对称二叉树] ( 对称二叉树.md )
54- 3.
58+ <!--
55594. [二叉树的层次遍历 II](二叉树的层次遍历II.md)
56605. [将有序数组转换为二叉搜索树](将有序数组转换为二叉搜索树.md)
57- 6 . [ 平衡二叉树] ( 平衡二叉树.md )
58- 7 . [ 二叉树的最小深度] ( 二叉树的最小深度.md )
61+ -->
Original file line number Diff line number Diff line change 1+ # 257.二叉树的所有路径
2+ ## 题目
3+ 给定一个二叉树,返回所有从根节点到叶子节点的路径。
4+
5+ 说明: 叶子节点是指没有子节点的节点。
6+
7+ ``` python
8+ 输入:
9+
10+ 1
11+ / \
12+ 2 3
13+ \
14+ 5
15+
16+ 输出: [" 1->2->5" , " 1->3" ]
17+
18+ 解释: 所有根节点到叶子节点的路径为: 1 -> 2 -> 5 , 1 -> 3
19+ ```
20+
21+ ## 分析
22+ #### 迭代
23+
24+ * 定义两个队列,一个队列存储节点,一个队列存储根到该节点的路径
25+ * 遍历每一个节点的同时,也在记录路径,直到叶子节点,将路径添加到返回列表中
26+
27+ ![ ] ( ../pic/leetcode_tree/257_1.gif )
28+
29+ ``` python
30+ from queue import Queue
31+ def binaryTreePaths (root ):
32+ if not root:
33+ return []
34+ node_queue = Queue()
35+ path_queue = Queue()
36+ node_queue.put(root)
37+ path_queue.put(str (root.val))
38+
39+ ans = []
40+ while node_queue.qsize():
41+ node = node_queue.get()
42+ path = path_queue.get()
43+ if node.left:
44+ node_queue.put(node.left)
45+ path_queue.put(path + ' ->' + str (node.left.val))
46+ if node.right:
47+ node_queue.put(node.right)
48+ path_queue.put(path + ' ->' + str (node.right.val))
49+ if node.left is None and node.right is None :
50+ ans.append(path)
51+ return ans
52+ ```
53+
54+ #### 递归
55+ ``` python
56+ def binaryTreePaths (root ):
57+ def construct_paths (root , path ):
58+ if root:
59+ path += str (root.val)
60+ if root.left is None and root.right is None :
61+ ans.append(path)
62+ else :
63+ path += ' ->'
64+ construct_paths(root.left, path)
65+ construct_paths(root.right, path)
66+
67+ ans = []
68+ construct_paths(root, ' ' )
69+ return ans
70+ ```
Original file line number Diff line number Diff line change 1+ # 572.另一个树的子树
2+ ## 题目
3+ 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。
4+ ``` python
5+ 给定的树 s:
6+ 3
7+ / \
8+ 4 5
9+ / \
10+ 1 2
11+ 给定的树 t:
12+ 4
13+ / \
14+ 1 2
15+ 返回 true,因为 t 与 s 的一个子树拥有相同的结构和节点值。
16+ ```
17+
18+ ## 分析
19+ 迭代,遍历s树,如果子树的根节点与t树的根节点相等,再去判断两个数是否相等,[ 相同的树] ( 相同的树.md )
20+ ``` python
21+ from queue import Queue
22+ def isSubtree (s , t ):
23+ def isSame (p , q ):
24+ if p is None and q is None :
25+ return True
26+ if not p or not q:
27+ return False
28+ if p.val != q.val:
29+ return False
30+ else :
31+ return isSame(p.left, q.left) and isSame(p.right, q.right)
32+
33+ Q = Queue()
34+ Q.put(s)
35+ while Q.qsize():
36+ node = Q.get()
37+ if node.val == t.val:
38+ if isSame(node, t):
39+ return True
40+ else :
41+ if node.left:
42+ Q.put(node.left)
43+ if node.right:
44+ Q.put(node.right)
45+ else :
46+ if node.left:
47+ Q.put(node.left)
48+ if node.right:
49+ Q.put(node.right)
50+ return False
51+ ```
Original file line number Diff line number Diff line change 1+ # 404.左叶子之和
2+ ## 题目
3+ 计算给定二叉树的所有左叶子之和。
4+ ``` python
5+ 3
6+ / \
7+ 9 20
8+ / \
9+ 15 7
10+
11+ 在这个二叉树中,有两个左叶子,分别是 9 和 15 ,所以返回 24
12+ ```
13+
14+ ## 分析
15+ #### 迭代
16+ 遍历树,如果一个节点的左节点是叶子节点,就累加
17+
18+ ``` python
19+ from queue import Queue
20+ def sumOfLeftLeaves (root ):
21+ def isLeavesNode (node ):
22+ # 判断一个节点是不是叶子节点
23+ if node.left is None and node.right is None :
24+ return True
25+ return False
26+
27+ if not root:
28+ return 0
29+ ans = 0
30+ q = Queue()
31+ q.put(root)
32+ while q.qsize():
33+ node = q.get()
34+ if node.left:
35+ if isLeavesNode(node.left):
36+ ans += node.left.val
37+ else :
38+ q.put(node.left)
39+
40+ if node.right:
41+ if not isLeavesNode(node.right):
42+ q.put(node.right)
43+ return ans
44+ ```
45+
46+ #### 深度优先搜索
47+ ``` python
48+ def sumOfLeftLeaves (root ):
49+ def isLeavesNode (node ):
50+ # 判断一个节点是不是叶子节点
51+ if node.left is None and node.right is None :
52+ return True
53+ return False
54+
55+ def dfs (root ):
56+ ans = 0
57+ if root.left:
58+ if isLeavesNode(root.left):
59+ ans += root.left.val
60+ else :
61+ ans += dfs(root.left)
62+ if root.right:
63+ if not isLeavesNode(root.right):
64+ ans += dfs(root.right)
65+ return ans
66+
67+ if not root:
68+ return 0
69+ return dfs(root)
70+ ```
Original file line number Diff line number Diff line change 1- ## 平衡二叉树
1+ # 110.平衡二叉树
2+ ## 题目
23给定一个二叉树,判断它是否是高度平衡的二叉树。一棵高度平衡二叉树定义为:
34
45** 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。**
910输出:true
1011```
1112
12- #### 代码
13- 自顶向下递归
13+ ## 分析
1414* 左右两个子树的高度差小于等于1
1515* 平衡二叉树的两个子树也是平衡二叉树
1616
17- ``` python
18- class Solution :
19- def isBalanced (self , root : TreeNode) -> bool :
20- def maxHeight (node ):
21- if not node:
22- return 0
23- return max (maxHeight(node.left), maxHeight(node.right)) + 1
24- if not root:
25- return True
26- left = root.left # 左子树
27- right = root.right # 右子树
28- if self .isBalanced(left) and self .isBalanced(right) and abs (maxHeight(left) - maxHeight(right)) <= 1 :
29- return True
30- return False
31- ```
32-
33- 自底向上(没懂)
34-
3517``` python
3618def isBalanced (root ):
37- def heigh (root ):
19+ def depth (root ):
20+ # 计算树的深度
3821 if not root:
3922 return 0
40- left_height = heigh(root.left)
41- right_height = heigh(root.right)
42- if left_height == - 1 or right_height == - 1 or abs (left_height- right_height) > 1 :
43- return - 1
44- else :
45- return max (left_height, right_height) + 1
46- return heigh(root) >= 0
23+ return 1 + max (depth(root.left), depth(root.right))
24+
25+ if not root:
26+ return True
27+ left = root.left
28+ right = root.right
29+ return isBalanced(left) and isBalanced(right) and abs (depth(left) - depth(right)) <= 1
30+
4731```
Original file line number Diff line number Diff line change 1+ # 513.找树左下角的值
2+ ## 题目
3+ 给定一个二叉树,在树的最后一行找到最左边的值。
4+ ``` python
5+ 输入:
6+
7+ 1
8+ / \
9+ 2 3
10+ / / \
11+ 4 5 6
12+ /
13+ 7
14+
15+ 输出:
16+ 7
17+ ```
18+
19+ ## 分析
20+ #### 迭代
21+ 层序遍历,队列中存放的都是同一层的节点,每一层第一个节点就是最左边的节点
22+
23+ ``` python
24+ from queue import Queue
25+ def findBottomLeftValue (root ):
26+ q = Queue()
27+ q.put(root)
28+ while q.qsize():
29+ ans = None
30+ size = q.qsize()
31+ for _ in range (size):
32+ node = q.get()
33+ if not ans:
34+ # 记录每一层最左边的节点
35+ ans = node
36+ if node.left:
37+ q.put(node.left)
38+ if node.right:
39+ q.put(node.right)
40+ return ans.val
41+ ```
You can’t perform that action at this time.
0 commit comments