labuladong / basic-challenge

200 stars 22 forks source link

已结束 #355

Closed labuladong closed 1 year ago

labuladong commented 1 year ago

本期打卡已结算完成。报名最新一期的打卡活动 点这里

ljrirene commented 1 year ago

https://leetcode.com/problems/diameter-of-binary-tree/solutions/3916238/543-diameter-of-binary-tree/

vanessacz commented 1 year ago
  1. Maximum Depth of Binary Tree: https://leetcode.com/problems/maximum-depth-of-binary-tree/solutions/3916278/java-traverse-backtracking-solution/
CarolSSS commented 1 year ago

https://leetcode.com/problems/binary-tree-preorder-traversal/solutions/3916293/recursion/

DannyT70 commented 1 year ago

https://leetcode.cn/problems/diameter-of-binary-tree/solution/er-cha-shu-1-by-sleepy-shawffi-ryhr/

alyssasuper3 commented 1 year ago

https://leetcode.cn/problems/maximum-depth-of-binary-tree/solutions/2391243/er-cha-shu-de-zui-da-shen-du-by-alyssasu-23d7/

KarlZhu-SE commented 1 year ago

https://leetcode.com/problems/diameter-of-binary-tree/solutions/3916890/recursion/

Sadwy commented 1 year ago

LC114. https://leetcode.com/problems/binary-tree-preorder-traversal/solutions/3917092/preorder-traversal/

AstrKing commented 1 year ago

104:https://leetcode.cn/problems/maximum-depth-of-binary-tree/solutions/2066936/er-cha-shu-de-zui-da-shen-du-by-aktrking-t52k/

cs-gavin-huang commented 1 year ago

https://leetcode.com/problems/diameter-of-binary-tree/solutions/3918179/diameter-of-binary-tree/

tonyzhu163 commented 1 year ago
# Aug 16

# Maximum Depth of a Tree
# There is either a recursive solution, or a traversal solution

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        return self.traverse(root, 0)

    def traverse(self, root, n):
        if not root:
            return n
        else:
            return max(self.traverse(root.left, n+1), self.traverse(root.right, n+1))

class Solution:
    def __init__(self,):
        self.max = 0

    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        self.traverse(root)
        return self.max

    def traverse(self, root):
        if not root:
            return
        else:
            left = self.depth(root.left)
            right = self.depth(root.right)
            self.max = max(self.max, left+right)

            self.traverse(root.left)
            self.traverse(root.right)

    def depth(self, root):
        if not root:
            return 0
        else:
            return 1 + max(self.depth(root.left), self.depth(root.right))
ElaineZhou-moon commented 1 year ago

https://leetcode.com/problems/maximum-depth-of-binary-tree/solutions/3918625/max-depth-of-binary-tree/

YilinYan0 commented 1 year ago

https://leetcode.com/problems/maximum-depth-of-binary-tree/post-solution/?submissionId=1023143448