Skip to content

Commit 262a6e5

Browse files
author
liningrui
committed
add new solutions
1 parent 3e45eec commit 262a6e5

28 files changed

+965
-9
lines changed

script/[109]有序链表转换二叉搜索树.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# head 中的节点数在[0, 2 * 10⁴] 范围内
3030
# -10⁵ <= Node.val <= 10⁵
3131
#
32-
# Related Topics 树 二叉搜索树 链表 分治 二叉树 👍 729 👎 0
32+
# Related Topics 树 二叉搜索树 链表 分治 二叉树 👍 730 👎 0
3333

3434

3535
# leetcode submit region begin(Prohibit modification and deletion)
@@ -46,4 +46,23 @@
4646
# self.right = right
4747
class Solution:
4848
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
49+
def dfs(start, end):
50+
nonlocal head
51+
if start>end:
52+
return None
53+
mid = (start+end)//2
54+
55+
left= dfs(start, mid-1)
56+
root = TreeNode(head.val)
57+
head = head.next
58+
right = dfs(mid+1, end)
59+
root.left = left
60+
root.right = right
61+
return root
62+
counter = 0
63+
cur = head
64+
while cur:
65+
cur = cur.next
66+
counter+=1
67+
return dfs(1, counter)
4968
# leetcode submit region end(Prohibit modification and deletion)

script/[110]平衡二叉树.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
# 树中的节点数在范围 [0, 5000] 内
3838
# -10⁴ <= Node.val <= 10⁴
3939
#
40-
# Related Topics 树 深度优先搜索 二叉树 👍 1075 👎 0
40+
# Related Topics 树 深度优先搜索 二叉树 👍 1079 👎 0
4141

4242

4343
# leetcode submit region begin(Prohibit modification and deletion)
@@ -49,4 +49,21 @@
4949
# self.right = right
5050
class Solution:
5151
def isBalanced(self, root: TreeNode) -> bool:
52+
def dfs(root):
53+
if not root:
54+
return 0
55+
left = dfs(root.left)
56+
right = dfs(root.right)
57+
if left ==-1 or right==-1:
58+
return -1
59+
elif abs(right-left)>1:
60+
return -1
61+
else:
62+
return max(left,right)+1
63+
result = dfs(root)
64+
if result==-1:
65+
return False
66+
else:
67+
return True
68+
5269
# leetcode submit region end(Prohibit modification and deletion)

script/[114]二叉树展开为链表.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,16 @@ def flatten(self, root: TreeNode) -> None:
5555
"""
5656
Do not return anything, modify root in-place instead.
5757
"""
58+
pre = None
59+
def dfs(root):
60+
nonlocal pre
61+
if not root:
62+
return None
63+
dfs(root.right)
64+
dfs(root.left)
65+
root.left = None
66+
root.right= pre
67+
pre = root
68+
dfs(root)
69+
return pre
5870
# leetcode submit region end(Prohibit modification and deletion)

script/[129]求根节点到叶节点数字之和.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,17 @@
5858
# self.right = right
5959
class Solution:
6060
def sumNumbers(self, root: TreeNode) -> int:
61+
total = 0
62+
def dfs(root, path):
63+
nonlocal total
64+
if not root.left and not root.right:
65+
path = 10*path + root.val
66+
total+=path
67+
return
68+
if root.left:
69+
dfs(root.left, 10*path+root.val)
70+
if root.right:
71+
dfs(root.right, 10*path+root.val)
72+
dfs(root, 0)
73+
return total
6174
# leetcode submit region end(Prohibit modification and deletion)

script/[236]二叉树的最近公共祖先.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
# p != q
4040
# p 和 q 均存在于给定的二叉树中。
4141
#
42-
# Related Topics 树 深度优先搜索 二叉树 👍 1850 👎 0
42+
# Related Topics 树 深度优先搜索 二叉树 👍 1854 👎 0
4343

4444

4545
# leetcode submit region begin(Prohibit modification and deletion)
@@ -52,5 +52,20 @@
5252

5353
class Solution:
5454
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
55+
def dfs(root):
56+
#返回的是节点
57+
if not root:
58+
return None
59+
if root==p or root==q:
60+
return root
61+
left = dfs(root.left)
62+
right =dfs(root.right)
63+
if left and right:
64+
return root
65+
elif left:
66+
return left
67+
else:
68+
return right
69+
return dfs(root)
5570

5671
# leetcode submit region end(Prohibit modification and deletion)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# 给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。
2+
#
3+
# 树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。
4+
#
5+
#
6+
#
7+
# 示例 1:
8+
#
9+
#
10+
#
11+
#
12+
# 输入:root = [1,null,3,2,4,null,5,6]
13+
# 输出:[[1],[3,2,4],[5,6]]
14+
#
15+
#
16+
# 示例 2:
17+
#
18+
#
19+
#
20+
#
21+
# 输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,
22+
# null,13,null,null,14]
23+
# 输出:[[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
24+
#
25+
#
26+
#
27+
#
28+
# 提示:
29+
#
30+
#
31+
# 树的高度不会超过 1000
32+
# 树的节点总数在 [0, 10^4] 之间
33+
#
34+
# Related Topics 树 广度优先搜索 👍 304 👎 0
35+
36+
37+
# leetcode submit region begin(Prohibit modification and deletion)
38+
"""
39+
# Definition for a Node.
40+
class Node:
41+
def __init__(self, val=None, children=None):
42+
self.val = val
43+
self.children = children
44+
"""
45+
46+
class Solution:
47+
def levelOrder(self, root: 'Node') -> List[List[int]]:
48+
if not root:
49+
return []
50+
result = []
51+
stack = [root]
52+
while stack:
53+
l = len(stack)
54+
tmp = []
55+
for i in range(l):
56+
node= stack.pop(0)
57+
tmp.append(node.val)
58+
for i in node.children:
59+
stack.append(i)
60+
result.append(tmp)
61+
return result
62+
63+
64+
65+
# leetcode submit region end(Prohibit modification and deletion)

script/[450]删除二叉搜索树中的节点.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,28 @@
6565
# self.right = right
6666
class Solution:
6767
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
68+
def dfs(root):
69+
if not root:
70+
return None
71+
if root.val ==key:
72+
if not root.left and not root.right:
73+
return None
74+
elif not root.left:
75+
return root.right
76+
elif not root.right:
77+
return root.left
78+
else:
79+
cur = root.right
80+
left = root.left
81+
while cur.left:
82+
cur = cur.left
83+
cur.left = left
84+
return root.right
85+
elif root.val < key:
86+
root.right = dfs(root.right)
87+
else:
88+
root.left =dfs(root.left )
89+
return root
90+
return dfs(root)
91+
6892
# leetcode submit region end(Prohibit modification and deletion)

script/[543]二叉树的直径.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。
2+
#
3+
#
4+
#
5+
# 示例 :
6+
# 给定二叉树
7+
#
8+
# 1
9+
# / \
10+
# 2 3
11+
# / \
12+
# 4 5
13+
#
14+
#
15+
# 返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。
16+
#
17+
#
18+
#
19+
# 注意:两结点之间的路径长度是以它们之间边的数目表示。
20+
# Related Topics 树 深度优先搜索 二叉树 👍 1097 👎 0
21+
22+
23+
# leetcode submit region begin(Prohibit modification and deletion)
24+
# Definition for a binary tree node.
25+
# class TreeNode:
26+
# def __init__(self, val=0, left=None, right=None):
27+
# self.val = val
28+
# self.left = left
29+
# self.right = right
30+
class Solution:
31+
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
32+
maxl = 0
33+
def dfs(root):
34+
nonlocal maxl
35+
if not root:
36+
return 0
37+
left = dfs(root.left)
38+
right =dfs(root.right)
39+
maxl = max(maxl, left+right)
40+
return max(left, right)+1
41+
dfs(root)
42+
return maxl
43+
44+
# leetcode submit region end(Prohibit modification and deletion)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 给定一个 N 叉树,找到其最大深度。
2+
#
3+
# 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
4+
#
5+
# N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。
6+
#
7+
#
8+
#
9+
# 示例 1:
10+
#
11+
#
12+
#
13+
#
14+
# 输入:root = [1,null,3,2,4,null,5,6]
15+
# 输出:3
16+
#
17+
#
18+
# 示例 2:
19+
#
20+
#
21+
#
22+
#
23+
# 输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,
24+
# null,13,null,null,14]
25+
# 输出:5
26+
#
27+
#
28+
#
29+
#
30+
# 提示:
31+
#
32+
#
33+
# 树的深度不会超过 1000 。
34+
# 树的节点数目位于 [0, 10⁴] 之间。
35+
#
36+
# Related Topics 树 深度优先搜索 广度优先搜索 👍 293 👎 0
37+
38+
39+
# leetcode submit region begin(Prohibit modification and deletion)
40+
"""
41+
# Definition for a Node.
42+
class Node:
43+
def __init__(self, val=None, children=None):
44+
self.val = val
45+
self.children = children
46+
"""
47+
48+
class Solution:
49+
def maxDepth(self, root: 'Node') -> int:
50+
def dfs(root):
51+
if not root:
52+
return 0
53+
depth = 0
54+
for child in root.children:
55+
depth = max(depth, dfs(child))
56+
return depth+1
57+
return dfs(root )
58+
59+
# leetcode submit region end(Prohibit modification and deletion)

0 commit comments

Comments
 (0)