Skip to content

Commit 9e23725

Browse files
committed
14/12/2020
1 parent 343c4a8 commit 9e23725

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

Daily-challenge/Dec/12/README.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,27 @@ The values of the nodes in the tree are unique. <br>
4141

4242
## Code
4343
```python
44+
# Definition for a binary tree node.
45+
# class TreeNode:
46+
# def __init__(self, val=0, left=None, right=None):
47+
# self.val = val
48+
# self.left = left
49+
# self.right = right
4450
class Solution:
45-
def permuteUnique(self, nums):
46-
ans = [[]]
47-
for n in nums:
48-
new_ans = []
49-
for l in ans:
50-
for i in range(len(l)+1):
51-
new_ans.append(l[:i]+[n]+l[i:])
52-
if i<len(l) and l[i]==n: break
53-
ans = new_ans
54-
return ans
51+
def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode:
52+
def postorder(root):
53+
if not root: return 0, None
54+
55+
l, r = postorder(root.left), postorder(root.right)
56+
57+
if l[0] > r[0]:
58+
return l[0] + 1, l[1]
59+
elif l[0] < r[0]:
60+
return r[0] + 1, r[1]
61+
62+
else:
63+
return l[0] + 1, root
64+
65+
return postorder(root)[1]
5566
```
5667

Daily-challenge/Dec/12/sol.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
17
class Solution:
2-
def permuteUnique(self, nums):
3-
ans = [[]]
4-
for n in nums:
5-
new_ans = []
6-
for l in ans:
7-
for i in range(len(l)+1):
8-
new_ans.append(l[:i]+[n]+l[i:])
9-
if i<len(l) and l[i]==n: break
10-
ans = new_ans
11-
return ans
8+
def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode:
9+
def postorder(root):
10+
if not root: return 0, None
11+
12+
l, r = postorder(root.left), postorder(root.right)
13+
14+
if l[0] > r[0]:
15+
return l[0] + 1, l[1]
16+
elif l[0] < r[0]:
17+
return r[0] + 1, r[1]
18+
19+
else:
20+
return l[0] + 1, root
21+
22+
return postorder(root)[1]

0 commit comments

Comments
 (0)